How can I define an array of string in c then iterate with a loop through the items in the array?
So far I have
char myStrings[][10] = { "one", "two", "three", "four", "five" };
// do I need to specify the "10" maximum length?
// also does it automatically create a null ending character after the string?
int i = 0;
for( i = 0; i < ; i++)
{
// I need to pass each string to a function which accepts
// const char *
}
When you declare a char sequence with
"", null terminator is added.Edit – sizeof()
sizeof() returns the size of a variable. It doesn’t matter if the variable is an int, array or 2d array.
For example, see the following program
Which outputs (on my machine):
Since each element in the array has the same size (in our case declared to be 10 bytes) we can divide the sizeof the outer array by the sizeof any of its elements to get the number of constituent elements. The simplest option is to just take the first one!