why does a double subscripted array will solve the problem?
more importantly, what is the problem with the array of strings?
You haven't specifically said what errors are occuring, but I'm inclined to think that your program is segfaulting when it attempts to overwrite the different strings.
The following...
Code:
char *strings[ ] = { "XYE", "BCD", "CDE", "DEF" };
...for me, creates an array of pointers to characters, as opposed to an array of strings (which would be an array of character arrays).
Now, your string literals must be kept somewhere in your program, but because you haven't allocated memory for the string literals, they are being kept somewhere else. I don't know where, but I suspect that they are being retained in the code (?) instead of being on the heap where you can manipulate them.
If they are being kept in the code, then you can't change them, or write to them at all. I tried the following program which compiles with no errors...
Code:
#include <stdio.h>
int main(void)
{
char *blah[]={"blah", "blahblah"};
/* blah[0][1]='h'; */
printf("blah1:%s\nblah2:%s\n", blah[0], blah[1]);
printf("sizeof(blah1)==%d\nsizeof(blah2)==%d\n", sizeof(blah[0]), sizeof(blah[1]));
printf("sizeof(blah[0][1])==%d\nsizeof(blah)==%d\n", sizeof(blah[0][1]), sizeof(blah));
return 0;
}
and gives me...
Code:
[~/misc/tries@poseidon] Milord? gcc -o try2 try2.c
[~/misc/tries@poseidon] Milord? try2
blah1:blah
blah2:blahblah
sizeof(blah1)==4
sizeof(blah2)==4
sizeof(blah[0][1])==1
sizeof(blah)==8
[~/misc/tries@poseidon] Milord?
Where the size of blah1 and blah2 is 4 bytes, ie. the size of a pointer address whilst the size of blah[0][1] is 1 byte, ie. the size of a char.
Should I uncomment the second line in main, however, the program segfaults before any output which suggests that the program cannot change any of the strings because the memory that they reside in is protected.
If you want to write over the memory that the strings reside in, you would need to dynamically allocate them if you didn't wish to use a double array.