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.

Recommended Answers

All 3 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.