How to declare 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?
main()
{
int i, j;
int **array[10][10];
int **ptr = NULL;
for(i=0;i<10;i++)
{
for(j=0j<10;j++)
{
alloc_2D(&ptr, 10, 10);
array[i][j] = ptr;
}
}
//After I do this, how can I access the individual 2D array
//and then the individual elements of the 2D arrays?
}
void alloc_2D(float ***memory, unsigned int rows, unsigned int cols)
{
float **ptr;
*memory = NULL;
ptr = malloc(rows * sizeof(float*));
if(ptr == NULL)
{
status = ERROR;
printf("\nERROR: Memory allocation failed!");
}
else
{
int i;
for(i = 0; i< rows; i++)
{
ptr[i] = malloc(cols * sizeof(float));
if(ptr[i]==NULL)
{
status = ERROR;
printf("\nERROR: Memory allocation failed!");
}
}
}
*memory = ptr;
}