I want to define different arrays of strings in C, which then can be e.g. selected depending on some other value, i.e. like following:
char foo[][10] = {"Snakes", "on", "a", "Plane"};
char bar[][10] = {"Fishes", "in", "a", "Lake"};
char *choice;
if (flag == 1) {
choice = &foo;
} else if (flag == 2) {
choice = &bar;
}
printf("%s%s\n", choice[0] , choice[1]);
Expected result in case flag
is 1:
Snakeson
Expected result in case flag
is 2:
Fishesin
But the above code gives a segmentation fault
error, while I tried differnt definitions for char
, i.e. char*
and char**
. How to do it right? Is there a good tutorial on this matter, i.e. on pointers, arrays, what foo
exactly is in the above example...