0

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.

1
  • the case is ok, because that is a char type that you can use in a switch... I am assuming your argv is the argv customarily passed into main, which is an array of strings or **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 at man 3 getopt Commented Feb 13, 2016 at 4:50

3 Answers 3

1

In switch, the expression must be of "an integral type"

Do a set of if/else instead of switch.

Sign up to request clarification or add additional context in comments.

3 Comments

The expression "shall have integer type". The question is tagged C, and C doesn't have class types or maps.
@KeithThompson Thanks for your comment, I have modified my answer
This doesn't really help unless you provide a code sample
0

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...
}

9 Comments

"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.
@KeithThompson an address is materialized as a number. It can freely be assigned to an integer type for example. The "comparable string" part is indeed confusing. I'll rephrase...
No, an address cannot be freely assigned to an integer type. It might (or might not) have a similar representation, and it can be converted with an explicit cast, but the language makes a strong distinction between pointers and numbers.
@KeithThompson - I'll cherry pick the part the part to support my argument: "Any pointer type may be converted to an integer type". Of course, thats not always meaningful and the other direction (int -> pointer) is more dangerous, but it is assignable.
Yes, may be converted. You can't assign a pointer to an integer; you can convert a pointer to an integer (and the conversion may be non-trivial), and then assign the result. The most important point is this: pointers are not integers.
|
0

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)

1 Comment

argv[I] is a char*, not an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.