Hello.

Im interested in learning something about allocating memory for a 2d array.
So I have a function that does this, and it goes something like this :

short **g2D(short **arr,short row,short col)    
{                                                    
    short i;
    arr=malloc(row*sizeof*arr);
    if(!arr)
    {
        printf("\nMemory unavailable");
        exit(EXIT_FAILURE);
        }

    for(i=MIN;i<row;i++)
    {
        arr[i]=malloc(col*sizeof*arr);

        if(!arr)
        {
            printf("\nMemory unavailable");
            exit(EXIT_FAILURE);
            }
    }
    return arr;
}

And the function call

2darray=g2D(2darray,row,col);

I would like to know what does it mean when I use a pointer to pointer in function prototype and in function declaration, is this necessary and is there another way to allocate memory for a 2d array via function? I understand the fact that dimensions dont exist in memory.

Edited 5 Years Ago by Joey_Brown: n/a

Well, the function is basically allocating memory to a bunch of pointers. It can be shown like this:

[....] means allocate memory to a pointer.

ptr1 = [.....]
ptr2 = [.....]
ptr3 = [.....]
ptr4 = [.....]

This is done by 
   for(i=MIN;i<row;i++)
    {
        arr[i]=malloc(col*sizeof*arr);

        if(!arr)
        {
            printf("\nMemory unavailable");
            exit(EXIT_FAILURE);
            }
    }

            _____________________________
ptr_ptr --> | ptr1 | ptr2 | ptr3 | ptr4 |
            -----------------------------
This is done by 
arr = malloc(row * sizeof(*arr));

Here, ptr_ptr has to be a pointer to a pointer. So, what we are doing is,

1) Allocate sizeof( pointer ) * 4 (4 pointers in the example)

2) Now, allocate memory for each of these pointers.

Edited 5 Years Ago by myk45: n/a

Well, the function is basically allocating memory to a bunch of pointers. It can be shown like this:

[....] means allocate memory to a pointer.

ptr1 = [.....]
ptr2 = [.....]
ptr3 = [.....]
ptr4 = [.....]

This is done by 
   for(i=MIN;i<row;i++)
    {
        arr[i]=malloc(col*sizeof*arr);

        if(!arr)
        {
            printf("\nMemory unavailable");
            exit(EXIT_FAILURE);
            }
    }

            _____________________________
ptr_ptr --> | ptr1 | ptr2 | ptr3 | ptr4 |
            -----------------------------
This is done by 
arr = malloc(row * sizeof(*arr));

Here, ptr_ptr has to be a pointer to a pointer. So, what we are doing is,

1) Allocate sizeof( pointer ) * 4 (4 pointers in the example)

2) Now, allocate memory for each of these pointers.

Thank you for your answer. I understand this, but Im curious about this part

short **g2D

Why do we need (or dont need) to declare pointer to pointer when allocating memory for 2d array ?

Well, it's not really a two-dimensional array -- it's a pointer to (the first element of an array of) pointer to (the first element of an array of) short.

The comp.lang.c FAQ, section 6, might help you understand, especially question 6.16.

This question has already been answered. Start a new discussion instead.