How to allocate a 2-d array of strings............By that I mean if t[][]
is the array
{char t[0][0]}
should store a string,
{ char t[0][1] }
should store a string etc........Can we use {char ***t }
to accomplish this ..If so how should I approach it?? Or can we also go about doing it like
{ char **t[10] }
, where 10 is the maximum length of any string I am going to enter in the array...
-
2Can we assume you are still trying to get the format proper? – alk Dec 26 '13 at 17:22
-
1Please also at least consider using a more advanced data structure for this. Your tag is C, but if you're really using C++, consider `std::string` and `std::vector`, etc. If really C, you can still create your own sparse array data structure that could be much more space-efficient, readable, maintainable and safer than higher-dimensional character arrays. – TypeIA Dec 26 '13 at 17:29
-
@Oren That's a 2D array of `char`s what set up in the answer you linked. The OP asks how to create a 2D array of "strings", that is a 3D array of `chars`. – alk Dec 26 '13 at 17:30
-
1@DavidNorris From where did you get the idea that he's using C++? Absolutely **nothing** suggests that. – Dec 26 '13 at 17:30
-
A missionary ... ;-) @H2CO3 – alk Dec 26 '13 at 17:32
-
@H2CO3 Yes, which is why I said "IF you're using C++" right after acknowledging that "Your tag is C." Read. – TypeIA Dec 26 '13 at 17:32
3 Answers
Edit:
To do this you will have to specify the cardinal of the second dimension and the maximum lenght of the string. Then use the name of the array with sizeof
and the number of strings you want to allocat for:
#include <stdio.h>
#include <string.h>
int main(void) {
/* 10 is the lenght of each string and 1 for the '\0'
10 is the number of strings per each 2D array
5 is the number of 2D arrays */
char (*array)[10][10+1] = malloc(5*sizeof(*array));
// Exemple
strcpy(array[0][0], "hello, world");
printf("%s\n", array[0][0]);
return 0;
}

- 7,124
- 6
- 27
- 36
well first of all you said: By that I mean if t[][] is the array {char t[0][0]} should store a string, { char t[0][1] }
, if t[0][1] would store a string then it's not a 2D array you want but rather a 3D array , in a 2D array it's t[0] that stores the string (because a string is an array , and 2D arrays are arrays of arrays) , having said that I'm going to show you how to Dynamically allocate memory for a 2D array and you can use the principle to create a 3D one.
char **matrix = NULL;
int i ;
matrix = malloc(sizeof(char) * ROWS);
for(i = 0 ; i < COLUMNS ; i++)
matrix[i] = malloc(sizeof(char));
and there you have it , just don't forget to use free
after you're done with that array
Edit :
to free a dynamically allocated 2D array you need to free
the last thing you malloced first , like this :
for(i = 0 ; i < COLUMNS ; i++)
free(matrix[i]);
free(matrix);

- 1,657
- 9
- 15
You have to allocate an array of pointers to pointers to char, which is:
char ***array = (char ***)malloc(sizeof(char**)*ARRAY_X);
Then, you have to allocate each single array of pointers to chars:
for(int i = 0; i < ARRAY_X; i++){
array[i] = (char **) malloc(sizeof(char *)*ARRAY_Y);
}
Finally you have to allocate strings:
for(int i = 0; i < ARRAY_X; i++){
for(int j = 0; j < ARRAY_Y; j++){
array[i][j] = (char *) malloc(sizeof(char)*ARRAY_Z);
}
}
ARRAY_X
and ARRAY_Y
and ARRAY_Z
are int indicating the dimension of the 2-d array of strings (which is a 3-d array of chars).

- 3,562
- 3
- 25
- 47
-
-
2**[Do NOT cast the return value of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858).** And better use `sizeof(*p)` instead of `sizeof(type)` for obvious safety reasons. – Dec 26 '13 at 17:31