Reputation: 6543
How can I declare an array of char* arrays? (when values will be filled later on in my program..) This is my goal:
arr[0] = {"0a","0b","0c"};
arr[0][0] = "0a";
arr[0][1] = "0b";
..
arr[1] = {"1a","1b","1c"}
char *arr[][]
? I feel pretty much confused.Upvotes: 0
Views: 1935
Reputation: 43518
void fill(char *arr[10][2]) {
arr[0][0] = strdup("k");
arr[0][1] = strdup("ki");
arr[1][0] = strdup("s");
arr[1][1] = strdup("si");
}
void clean(char *arr[10][2]) {
free(arr[0][0]);
free(arr[0][1]);
free(arr[1][0]);
free(arr[1][1]);
}
int main (int argc, char *argv[]){
char *array[10][2] = {NULL};
fill(array);
printf("%s %s\n", array[0][0], array[1][1]);
// when arr[i][j] bcome useless in your code, do not forget to free memories
clean(array);
}
the fill
and clean
functions could defined as well in the following way:
void fill(char *arr[][2]) {
void clean(char *arr[][2]) {
Upvotes: 0
Reputation: 43518
int main (int argc, char *argv[]){
char arr[10][2][3] = {0};
memcpy(&arr[0][0], "k", sizeof(arr[0][0]));
memcpy(&arr[0][1], "ki", sizeof(arr[0][0]));
memcpy(&arr[1][0], "s", sizeof(arr[0][0]));
memcpy(&arr[1][1], "si", sizeof(arr[0][0]));
printf("%s %s\n", arr[0][0], arr[1][1]);
}
Upvotes: 0
Reputation: 12985
Any of these. Note that this is one thing almost everyone gets confused about in C.
char ** a;
Here a
is a pointer to one or more pointers to one or more characters. There are a bunch of pointers somewhere in memory and each one points to a string (so to speak). a
refers to the first of these pointers. (a+1)
refers to the 2nd one. The blocks of characters each reside somewhere in memory but not necessarily anywhere near each other. You might do this:
for (int i = 0 ; i < 10 ; ++i) {
a++ = malloc(100);
}
or you could do this:
char *b = malloc(1000);
for (int i = 0 ; i < 10 ; ++i) {
a++ = b;
b += 100;
}
And you will have filled in the array with 10 pointers to 100 characters each. (Well, really spaces for 100 characters since we didn't fill in any characters yet.)
char* a[];
Here, again, a
is a bunch of pointers to the first of a bunch of characters. It's really the same as char ** a
but makes more sense if you are going to reuse them all.
for (int i = 0 ; i < 10 ; ++i) {
a[i] = malloc(100);
}
which will do the same thing as above but leaves it easier to get back to any given block of characters. For example, we could do strcpy(a[4], "Bob was here");
char a[][];
This is a little different in that it is a pointer to a bunch of characters that can be broken up into blocks of the same length. You would typically give sizes in this case unless its too big to allocate on the stack. char a[10][100]
would allocate 1000 character spots in memory with them divided into 10 groups of 100 each.
Notice that your example {"0a","0b","0c"};
is a way to allocate three blocks of memory. Each is three bytes long and is pre-populated with the characters there (and a closing \0 character.
Upvotes: 0
Reputation: 409166
Is it char *arr[][] ?
Sound about right. But you need to provide sizes for the arrays in the declaration.
Upvotes: 1
Reputation: 2908
Close, but you have to use aggregate initialization all in one shot, like so:
char arr[][2][3] =
{
{"0a", "0b"},
{"1a", "1b"},
};
Note that all but the most significant (left-most) array must be explicitly sized. Or you could declare each internal string as just a pointer:
const char * arr[][2] =
{
{"0a", "0b"},
{"1a", "1b"},
};
It depends on how structured your data is, and how static. An alternative is to use stl std::vector if you want to dynamically assign the contents of each array.
Upvotes: 2