// Dynamic allocation of two dimensional arrays
// An example of dynamic allocation of a two-dimensional array (matrix). An array
// of ARRAYSIZE * ARRAYSIZE integers is allocated. The elements of the array are
// then filled and printed out to prove that it worked.
#include
#include
#define ARRAYSIZE 10
int ** array;
int i,j;
main ()
{
array = (int **) malloc (ARRAYSIZE * sizeof (int *) );
if ( array) {
for (i = 0; i < ARRAYSIZE; i++) {
array[i] = (int *) malloc (ARRAYSIZE * sizeof (int) );
if (! array[i]) {
printf ("Not enough memory\n");
exit(1);
}
}
for (i = 0; i < ARRAYSIZE; i++)
for (j = 0; j < ARRAYSIZE; j++)
array[i][j] = i;
for (i = 0; i < ARRAYSIZE; i++) {
for (j = 0; j < ARRAYSIZE; j++)
printf ("%d", array[i][j]);
printf ("\n");
}
}
}