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
Can I modify strings in an array of strings in C?
Can I sort an array of strings in C?
How can I take user input for an array of strings in C?
words_array[0]=word1;
word_array[0] is a char, whereas word1 is a char *. Your character is not able to hold an address.
An array of strings might look like it:
char array[NUMBER_STRINGS][STRING_MAX_SIZE];
If you rather want an array of pointers to your strings:
char *array[NUMBER_STRINGS];
And then:
array[0] = word1;
array[1] = word2;
array[2] = word3;
Maybe you should read this.
If you need an array of strings. There are two ways:
1. Two Dimensional Array of characters
In this case, you will have to know the size of your strings beforehand. It looks like below:
// This is an array for storing 10 strings,
// each of length up to 49 characters (excluding the null terminator).
char arr[10][50];
2. An array of character pointers
It looks like below:
// In this case you have an array of 10 character pointers
// and you will have to allocate memory dynamically for each string.
char *arr[10];
// This allocates a memory for 50 characters.
// You'll need to allocate memory for each element of the array.
arr[1] = malloc(50 *sizeof(char));
I know that strings are char arrays already and I was wondering if there's a way to store not strings in an array. It would be like storing arrays into an array and that sounds a bit weird.
As far as I know an array is a pointer so I'd tend to say no, but after all pointers are just storing numbers like any variable.
To maybe be clearer, what I'd want to do is have an array that's like that :
array[0] = BLUE
array[1] = RED
array [2] = YELLOW
etc
suppose I have this line of code:
char myarr[10][0];
myarr[1][0] = "test";
printf("myarr: %c\n",myarr[1][0]);
Why does this give an error and how do I assign string to myarr?