How to declare a 2D array of 2D array pointers and access them?

Posted by vikramtheone on Stack Overflow See other posts from Stack Overflow or by vikramtheone
Published on 2010-05-27T14:12:07Z Indexed on 2010/05/27 14:51 UTC
Read the original article Hit count: 229

Hi Guys, How can I declare an 2D array of 2D Pointers? And later access the individual array elements of the 2D arrays.

Is my approach correct?


void alloc_2D(int ***memory, unsigned int rows, unsigned int cols);

int main()
{
 int i, j;
 int **ptr;
 int **array[10][10];


 for(i=0;i<10;i++)
 {
    for(j=0;j<10;j++)
     {
        alloc_2D(&ptr, 10, 10);
        array[i][j] = ptr;
     }
  }


//After I do this, how can I access the 10 individual 2D arrays?

 return 0;
}

void alloc_2D(int ***memory, unsigned int rows, unsigned int cols)

{

int **ptr;

*memory = NULL;

ptr = malloc(rows * sizeof(int*));

if(ptr == NULL)
{
    printf("\nERROR: Memory allocation failed!");

}
else
{
    int i;

    for(i = 0; i< rows; i++)
    {
        ptr[i] = malloc(cols * sizeof(float));

        if(ptr[i]==NULL)
        {

            printf("\nERROR: Memory allocation failed!");
        }
    }

}

*memory = ptr;
 }

© Stack Overflow or respective owner

Related posts about c

    Related posts about pointers