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 }
The canonical way:
Note: This only works on the array itself, as soon as you have a pointer (such as if you pass
argv_customto a function), it no longer works: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.