2

How can I use switch case with array in C? I know I can use if statement to fulfill my need like:

char *choice[]={'option1', 'option2'}
    if (strcmp choice[0], input)==0){
        // do the job
    }
    else{
        // something
    }

So the reason I wrote this is because I have seen people giving some good guides here but its not what the person asked for so just wanted to clarify that I know the concepts its just I to get the hints or relevant example to see how i can use switch case with array in C language. There are some places which says switch cannot use array strings so want to clarify if thats true.

4
  • I just realized that I didn't understand your question ^^' Commented Apr 1, 2018 at 6:41
  • You can use switch(expression) if and only if expression have integer type. Since a C style string doesn't have integer type, you can't do a switch on strings. There is no workaround, i.e. use strcmp for comparing strings Commented Apr 1, 2018 at 6:43
  • 1
    OT: char *choice[]={'option1', 'option2'} is wrong. Use " instead of ' Commented Apr 1, 2018 at 6:49
  • I put another answer here: stackoverflow.com/a/49596640/694576 Commented Apr 1, 2018 at 9:26

3 Answers 3

0

No, you cannot use strings (nor arrays of any kind) in a switch statement. As per the C11 standard,

The controlling expression of a switch statement shall have integer type.

This is a consequence of how the switch statement is implemented. Although not imposed by the standard, one common way of implementing the switch statement is to use an array of pointers to code, and place, at each possible case value, where are the instructions that need to be executed. This is also the reason why if falls through: once the case is found, it starts executing instructions from the location indicated by the array I mentioned before and there is no mechanism to stop.

Since you cannot use arrays to index arrays, they are forbidden from the switch statement.

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

1 Comment

Thanks i needed that clarified. apart from switch and if statement is there any other alternative can be used for array string? @paul92
0

you can only pass integer value in param of switch in C

int i = 0;
switch(i){
 case 0: // do something if i is 0
 break;
 case 1: // do something if i is 1
 break;
 default: // if no case matched run code in default
 break;
}

However you can use some hack like passing a char in switch which will alternatively be converted to int ASCII on backend

char *choice[]={'option1', 'option2'}
switch(choice[0][0]){
 case 'o': // the first array element with 'o'
 break;
}

2 Comments

in the switch why does choice have 2 0s?
how did you convert the char into integer here?
0

How can I use switch case with array in C

C is not a language with a lot of feature, the expression inside a switch is evaluated like every other expression in C. switch require an expression that return an integer. In the case of string, in C we use strcmp() but this function return an big range of value to be usable in a switch statement. That why strcmp() is not a very good function to show a good use of switch, here a better exemple:

#include  <stdio.h>

enum foo {
    EQUAL,
    LESS,
    GREATER,
};

enum foo cmp_int(int a, int b) {
    if (a < b) {
        return LESS;
    }
    else if (a > b) {
        return GREATER;
    }
    else {
        return EQUAL;
    }
}

void magic(int a, int b) {
    switch (cmp_int(a, b)) {
      case LESS:
        printf("%d is less than %d\n", a, b);
        break;
      case GREATER:
        printf("%d is greater than %d\n", a, b);
        break;
      case EQUAL:
        printf("%d is equal to %d\n", a, b);
        break;
    }
}

int main(void)
{
    magic(1, 2);
    magic(5, -5);
    magic(0, 0);
}

Output:

1 is less than 2
5 is greater than -5
0 is equal to 0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.