C: Iterate through an array of strings
c
Solution
Now, how would I go about determining the number of strings within argv_custom?
The canonical way:
int argc_custom = sizeof(argv_custom) / sizeof(argv_custom[0]);
Note: This only works on the array itself, as soon as you have a pointer (such as if you pass `argv_custom` to a function), it no longer works:
char **p = argv_custom;
int argc_custom = sizeof(p) / sizeof(p[0]); // No me gusta
is it possible to do something like: ...
There's no shortcut. You would have to iterate over each string, and do `strcmp`. Of course, you could (and probably should) wrap this behaviour into a function.
Problem
I'm currently learning C and am struggling with how to iterate through an array of strings. Let's just say I define an array, like so: ``` char* argv_custom[] = {"--debug", "--verbose", "--test", "--ultimate"}; ``` Now, how would I go about determining the number of strings within argv_custom? (e.g. argc_custom) Am I going the right way about this in the first place? Also, is it possible to do something like: Pseudocode ``` if ('--debug' in argv_custom) { // do stuff } ```