3

I am using the below code snippet to allocate memory for 2D array using minimum number of malloc() calls.

I want to access the array using subscripts, p[i][j].

#define ROW 3
#define COL 2

int main()
{
    void **ptr = malloc( ROW*COL* sizeof(int) );

    int (*p)[COL] = ptr;

    int i, j;

    for( i = 0; i < ROW; ++i )
            for( j = 0; j < COL; ++j )
                    scanf("%d", &ptr[i][j]);

    for( i = 0; i < ROW; ++i )
    {
            for( j = 0; j < COL; ++j )
                    printf("%d ", p[i][j]);
            printf("\n");
    }

    return 0;
}

The program is outputting correctly whatever is the input.

But, it is showing Runtime error . Why?

Green goblin
  • 9,898
  • 13
  • 71
  • 100

2 Answers2

7

If the array dimensions are known at compile-time (as in your example), then you can indeed allocate memory in one malloc call. But you have to use the proper pointer type to access that memory. In your case that would be your p pointer. You p pointer is declared correctly, but you for some reason are completely ignoring its existence in scanf and using ptr instead.

Stop trying to use ptr for array access. Use p. Access your array elements as p[i][j] and it should work.

In fact, I would get rid of ptr entirely and do memory allocation in the following way

int (*p)[COL] = malloc(ROW * sizeof *p);

Moreover, since both dimensions are known at compile time, you can actually allocate it as

int (*p)[ROW][COL] = malloc(sizeof *p);

but in this case you'll have to remember to access the array as (*p)[i][j] (note the *). Choose whichever method you prefer.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

Try this one:

#define ROW 3
#define COL 2

int main()
{
    int *ptr = (int*)malloc( ROW*COL* sizeof(int) );

    int *p[COL];
    int i, j;

    fot (i = 0; i < COL; i++)
        p[i] = ptr + ROW * i;

    for( i = 0; i < ROW; ++i )
        for( j = 0; j < COL; ++j )
            scanf("%d", &p[i][j]);

    for( i = 0; i < ROW; ++i )
    {
            for( j = 0; j < COL; ++j )
                    printf("%d ", p[i][j]);
            printf("\n");
    }

    free(ptr);
    return 0;
}
Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
  • Er... You are trying to access `p[i]` through a completely uninitialized pointer `p`. This will simply crash (at best). – AnT stands with Russia Aug 29 '12 at 17:03
  • I am initializing the array of pointers 'p' in the first loop – Mahmoud Fayez Aug 29 '12 at 20:44
  • You cannot initialize anything, because you don't have the array yet. You `p` is declared as `int (*p)[COL]`. This declaration creates an *pointer to an array* (not an "array or pointers" as you incorrectly believe). This pointer is not initialized, i.e. it points to nowhere. And you are trying to apply `[]` operator to a pointer that points to nowhere. As I said above, this will crash hopelessly. Try it. – AnT stands with Russia Aug 29 '12 at 20:49
  • I will try it and if it is wrong I will correct/delete this answer. – Mahmoud Fayez Aug 29 '12 at 20:50
  • If you wanted to declare an "array of pointers", you should have used `int *p[COL]` (note: no braces). With braces `int (*p)[COL]` it declares `p` as *pointer to array*, not *array of pointers*. – AnT stands with Russia Aug 29 '12 at 20:51