How to declare a 2D array of 2D array pointers and access them?
- by vikramtheone
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;
}