I am using the below code snippet to allocate memory for 2D array using minimum number of malloc()
calls.
I want to access the array using subscripts, p[i][j].
#define ROW 3
#define COL 2
int main()
{
void **ptr = malloc( ROW*COL* sizeof(int) );
int (*p)[COL] = ptr;
int i, j;
for( i = 0; i < ROW; ++i )
for( j = 0; j < COL; ++j )
scanf("%d", &ptr[i][j]);
for( i = 0; i < ROW; ++i )
{
for( j = 0; j < COL; ++j )
printf("%d ", p[i][j]);
printf("\n");
}
return 0;
}
The program is outputting correctly whatever is the input.
But, it is showing Runtime error . Why?