The general idiom for distributing an array N to M of any type T is
T **a = malloc(N * sizeof *a); if (a) for (i = 0; i < N; i++) a[i] = malloc(M * sizeof *a[i]);
According to the 1989 standard, you do not need to indicate the result of malloc , and in fact it is considered bad practice (it can suppress useful diagnostics if you forget to include stdlib.h or otherwise put on a prototype for malloc in the area). Early versions of C had a malloc return char * , so casting was necessary, but the likelihood that you would have to work with the compiler before 1989 was pretty far removed. C ++ requires execution, but if you write C ++, you must use the new operator.
Secondly, note that I am applying the sizeof operator to the selected object; the type of the expression *a is T * , and the type *a[i] is T (where in your case T == char ). This way you don't have to worry about keeping the sizeof expression in sync with the type of the selected object. IOW, if you decide to use wchar instead of char , you only need to make this change in one place.
John bode
source share