How can I use a char array in a switch statement?
If I do it this way,
switch (argv[i]) {
case '-': .....
default: ......
}
I get an error:
switch quantity not an integer.
In switch, the expression must be of "an integral type"
Do a set of if/else instead of switch.
You can't really do that.
argv[i] is a memory address, a number, it's not a string and can't be implicitly compared to another "string" (either literal of char array). Withing a switch statement, only integral type can be compared. You can use a specific character inside that "string":
switch(argv[i][0]){
case '-': .....
default: ......
}
but that's probably not what you want...
The straight forward solution is to use a group of if() ... else if() ... statements:
if(!strcmp(argv[i], "-")) {
//...
} else if(!strcmp(argv[i], "some other value")) {
//...
} else {
// non of these...
}
argv[i] is a memory address" ... right ... ", a number" -- nope. An address is not a number. And I'm not sure what you mean by "comparable string". The expression in a switch statement has to be of integer type.According to C11:
6.8.4.2 The switch statement
1 The controlling expression of a switch statement shall have integer type.
So unfortunately, one can never use non-integer types in a switch statement.
Also, argv[1] is a char* actually. This is easy to understand, because int main(int argc, char *argv[]) is just equivalent to int main(int argc, char **argv)
argv[I] is a char*, not an array.
**char... you can't do:switch(char*)it just is the way the language is structured... if you are working on parsing argv for normal switches, you should look atman 3 getopt