You can do this:
Code:
#define STRLEN 7
char *mat = malloc(num*STRLEN);
You would then access (eg) the third string this way:
Code:
strcpy(mat + 2*STRLEN, "hello\n");
Ie, you cannot use the index [] operator. For that, you do have to malloc both levels:
Code:
char **mat = malloc(num * sizeof(char*));
for (i = 0; i < num; i++) {
mat[i] = malloc(STRLEN);
}
And you have to free() each of those individually before you free(mat). However, it makes everything else easier because you can refer to each string with [].
Nb. that's sizeof(char*), not sizeof(char). Ask if you do not understand the difference. You could use STRLEN*sizeof(char) in the for loop, altho just "STRLEN" is fine unless you are on some very bizarre OS where a char != a byte.