You can only recover this information if you have the str_table
variable available. In that case you can write:
sizeof(str_table) / sizeof(str_table[0])
If you are trying to work out the length in a function that receives the array as a parameter, it is too late. At that point you cannot recover the information. If you are in that position, you will have to pass the length to the function, as well as the array.
The function might look like this:
void scan(const char* arr[], const size_t len)
{
for (size_t i=0; i<len; i++)
doSomething(arr[i]);
}
And you call it like this:
size_t len = sizeof(str_table) / sizeof(str_table[0]);
scan(str_table, len);
From inside the function, arr
is a pointer of type const char**
. No length information is provided.
Another approach to this problem is to null-terminate the array. Like this:
const char *str_table[] = { str0, str1, str2, str3, NULL };
This is exactly the same idea as is used with C strings. You can pass just the array to a function, and the function can iterate over the array until it encounters a null pointer.