How to declare array of strings in C.
Is it like
char str[100][100] ={"this","that","those"};
If so how to access the values .. can i travers like this?
(It does not give any compilation error ..but shows some additional garbage characters)
int i ,j;
char c[100][100] = {"this","that"};
for(i = 0 ;c[i] != '\0';++i)
for(j = 0; c[i][j] != '\0';++j)
printf("%c",c[i][j]);
Is it necessary to add ‘\0’ at end of eac string..for ex:
char c[100][100]={"this\0","that\0"}
It is Ok, but you will have to be extremely careful of buffer-overflow when dealing with these strings
Note that the condition in the first for loop:
for(i = 0 ;c[i] != '\0';++i)is probably wrong, and will fail sincec[i]is an array, whose address is not 0. You should probably iterate the outer array by numbers [until you read all elements], and not until you find some specific character. You can do that by maintaining a different variablen, which will indicate how many elements does the array currently have.No – the compiler add it to you, it is just fine without adding the
'\0'to the string.