How to declare 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:21 UTC
Read the original article
Hit count: 240
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;
}
© Stack Overflow or respective owner