Thread: switch case statement

  1. #1
    Registered User
    Join Date
    Jul 2004

    switch case statement

    I'm getting errors with the following switch case statement.

    /*switch (command_read)
    			case command_list[0]:
    			case command_list[1]:
    			case command_list[2]:
    			case command_list[3]:
    			case command_list[4]:
    I'm getting the following error:
    --------------------Configuration: IO - Win32 Debug--------------------
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(71) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(73) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(75) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(77) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(79) : error C2051: case expression not constant
    G:\Current Courses\EECE 314\projects\el_with_synchronization\IO\IO.CPP(81) : warning C4060: switch statement contains no 'case' or 'default' labels
    Error executing cl.exe.

    IO.exe - 5 error(s), 1 warning(s)
    I'm not entirely sure about what's wrong with the implementation using the array indices. Any help would be much appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    case labels should be constants
    use enum for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Right, the case-label needs to be an integer constant that the compiler knows at the compile-time. You are using an indexed array, which the compiler can't make into a constant at the time of compilation.

    Vart suggests an enum, which is one good choice, e.g. :
    enum somename {Value1, Value2, Value3};
       somename val;
           case Value1:
           case Value2:
           case Value3:
    If you have an array that contains some values, and you really want to switch on the content of the array, then you may want to use a loop to find the correct index and then switch on the index. Or you could make a two-element array:
    struct st {
       int val;
       char *str;
    static const st command_list[] =  { 
       { 0, "Hello" }, 
       { 1, "NHL" },
       { 2, "NFL" },
       for(i = 0; i < sizeof(command_list) / sizeof(command_list[0]); i++)
           if (command_list[i].val == command_read) 
               printf("%s\n", command_list[i].str);

    There may be other solutions that are better for what you want to do, but without understanding what you are actually trying to achieve, this is the best suggestions I can give.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Don't forget to put break on each case, otherwise it will continue to the next one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  2. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 01:16 PM
  3. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM
  4. A simple array question
    By frenchfry164 in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2001, 04:13 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM