I want to know how can I form a 2D array using double pointers?
Suppose my array declaration is:
char array[100][100];
How can I ge
Typical procedure for dynamically allocating a 2D array using a pointer-to-pointer::
#include
...
T **arr; // for any type T
arr = malloc(sizeof *arr * ROWS);
if (arr)
{
size_t i;
for (i = 0; i < ROWS; i++)
{
arr[i] = malloc(sizeof *arr[i] * COLS);
if (arr[i])
// initialize arr[i]
else
// panic
}
}
Note that since you're allocating each row separately, the contents of the array may not be contiguous.