Possible Duplicate:
How do I work with dynamic multi-dimensional arrays in C?
pointer to array type, c
If my C89 ANSI (e.g. not C99) C code declares a variable AND allocates memory using:
char myArray[30000][3];
Is there a way I can de-couple the declaration from the memory allocation by using malloc()
? For example (and pardon my newbie-ness):
char *myArray;
int i, arrayLength;
...
/* compute arrayLength */
...
myArray = malloc( sizeof(char) * arrayLength * 3);
for (i=0; ii<arrayLength; i++)
strncpy(myArray[i], "ab", 3);
...
free(myArray);
The goal is to create myArray
looking like, for example:
myArray[0] = "ab"
myArray[1] = "ab"
myArray[2] = "ab"
...
myArray[arrayLength-1] = "ab"
Is that the right approach?