I am trying to create an array of strings, and print it. Sounds easy, but nothing works for me.
First try was:
int main(){
char ls[5][3]={"aba","bab","dad","cac","lal"};
printf("Das hier: %s", ls[1]);
return 0;
}
But the output is:
Das hier: babdadcaclal a
Even if it should only be:
bab
Next try was after some searches, using:
char* ls[5][3]=.....
instead. This prints:
Das hier: pP@
I was searching for this problem about one day, and I guess, that the problem could be my compiler, but I am not sure about that...
Hope that someone knows what to do, because I am out of ideas. I am using the gcc compiler, if this somehow matters.
NULLterminators in your strings.char ls[][4]={"aba",...Then for the number of rows (in the scope wherelsis defined),size_t n = sizeof ls / sizeof *ls;.char *ls[] = {"aba", "bab", "dad", "cac", "lal" };(extra spaces for readability) That makes the array very easy to modify and an element can be displayed via:printf( "Das hier: %s\n", ls[1] );Please note the trailing '\n' in the format string, so the text is immediately output to the terminal rather than sitting in the stdout buffer until the program exits