Or something like this. I'm not sure about the allocation of memory for ptr. Though it seems to work well.
Code:
#include <stdio.h>
#include <stdlib.h>
#define COLS 2
#define ROWS 3
char **create_2d_array (int colums, int rows)
{
char **ptr;
int i;
for (i = 0; i < rows; i++)
{
ptr [i] = (char *) malloc (colums);
}
return ptr;
}
int main(int argc, char **argv)
{
char **array_2d;
int i, j;
array_2d = create_2d_array (COLS, ROWS);
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
array_2d [i][j] = 1;
}
}
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
array_2d [i][j] = 1;
printf ("%d ", array_2d [i][j]);
}
printf ("\n");
}
for (i = 0; i < ROWS; i++)
{
free (array_2d [i]);
}
return 0;
}