If you don't want to change the strings, then you could simply do
const char *a[2];
a[0] = "blah";
a[1] = "hmm";
When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".
If you do want to be able to change the actual string content, the you have to do something like
char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");
This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.
If you don't want to change the strings, then you could simply do
const char *a[2];
a[0] = "blah";
a[1] = "hmm";
When you do it like this you will allocate an array of two pointers to const char. These pointers will then be set to the addresses of the static strings "blah" and "hmm".
If you do want to be able to change the actual string content, the you have to do something like
char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");
This will allocate two consecutive arrays of 14 chars each, after which the content of the static strings will be copied into them.
There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:
char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1];
...
strcpy(strs[0], aString); // where aString is either an array or pointer to char
strcpy(strs[1], "foo");
You can add a list of initializers as well:
char strs[NUMBER_OF_STRINGS][STRING_LENGTH+1] = {"foo", "bar", "bletch", ...};
This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that's 20 characters long, 99 strings are going to have at least 15 unused characters; that's a waste of space.
Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:
char *strs[NUMBER_OF_STRINGS];
Note that in this case, you've only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc()). You can use the initializer list like the earlier example:
char *strs[NUMBER_OF_STRINGS] = {"foo", "bar", "bletch", ...};
Instead of copying the contents of the string constants, you're simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:
strs[i] = "bar";
strs[i] = "foo";
But you may not be able to change the string's contents; i.e.,
strs[i] = "bar";
strcpy(strs[i], "foo");
may not be allowed.
You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:
strs[i] = malloc(strlen("foo") + 1);
strcpy(strs[i], "foo");
BTW,
char (*a[2])[14];
Declares a as a 2-element array of pointers to 14-element arrays of char.
Videos
When you declare a char sequence with "", null terminator is added.
char myStrings[][10] = { "one", "two", "three", "four", "five" };
for (size_t i = 0; i < sizeof(myStrings) / sizeof(myStrings[0]); i++)
{
fooThatReceivesOneString(myStrings[i]);
}
Edit - sizeof()
sizeof() returns the size of a variable. It doesn't matter if the variable is an int, array or 2d array.
For example, see the following program
#include <stdio.h>
int main() {
char myStrings[][10] = { "one", "two", "three", "four", "five" };
printf("sizeof(myStrings): %zu\n", sizeof(myStrings));
printf("sizeof(myStrings[0]): %zu\n", sizeof(myStrings[0]));
return 0;
}
Which outputs (on my machine):
sizeof(myStrings): 50
sizeof(myStrings[0]): 10
Since each element in the array has the same size (in our case declared to be 10 bytes) we can divide the sizeof the outer array by the sizeof any of its elements to get the number of constituent elements. The simplest option is to just take the first one!
void loopftn (void)
{
char *numbers[] = {"One", "Two", "Three", ""}, **n;
n = numbers;
while (*n != "") {
printf ("%s\n", *n++);
}
return;
}
- You don't need to have a maximum length.
- All strings in C are null-terminated.