Passing string arrays requires more information...
This is similar to the input you would find in say a call to int main(int argc, char *argv[])
. The exception is that when main()
is called, it reads the command line and provides count information, in argc, to determine the number of arguments in argv[]
. That is why the char *argv[]
argument can be passed.
You have the same requirement. That is when passing an argument such as char *s[]
, i.e., you must also somehow provide a value telling func()
how many strings. C has no way of knowing the number of strings in that variable without being told. This is because an array reference ( char *s[]; ) decays into a pointer to char ( char *s; ) pointing to the first element of that array, i.e., no array size information.
So, the answer to your question is:, with the information given, func()
cannot determine the number of strings in s
.
An important distinction:
The size CAN be determined for character arrays, such as
char *s[]={"this","is","an","array"}; // the assignment of values in {...}
//make this an array of strings.
But only when s
is declared in the same scope of the function attempting to determine the number of elements. If that condition is met, then use:
int size = sizeof(s)/sizeof(s[0]); //only works for char arrays, not char *
//and only when declared and defined in scope of the call
In scope, in this context, simply means that char *s[]... be defined with global visibility, or within the function calculating number of elements. If passed as an argument, char *s[];, as defined above, will be simply seen as char *s, and the number of arguments cannot be determined.
Other options for retrieving count of strings in a string array include:
1) Modify your input array so that something in the last string is unique, like "%", or "\0". This will allow you to test strings with standard C string functions.
2) Include an argument in the function providing number of strings. (eg, similar to main(...)
or printf(...)
)