Thread: switch case with variables

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    29

    switch case with variables

    Why the following code doesn't give errors when I try to compile with gcc, and....

    Code:
    #include <stdio.h>
    int x = 2;
    const int y = 2;
    
    switch(x)
    {
         case(y)
               {
                  break;
               }
              default:
              break;
    }
    this one does?

    Code:
    #include <stdio.h>
    #include "remote_conf.hxx"
    
    
    int x = 2;
    auto_ptr<Remote> aXML (Remote_("../config/remote_conf.xml"));
    
    const int y =(const int)( aXML->y() );
    
    switch(x)
    {
         case(y)
               {
                  break;
               }
              default:
              break;
    }
    error: ‘y’ cannot appear in a constant-expression
    Last edited by frs; 11-28-2010 at 12:38 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > const int y = 2;
    This one is a const known at compile time.

    > const int y =(const int)( aXML->y() );
    This is a run-time initialisation. The 'const' comes in later, if you then try to assign something to y.

    case labels need to be known at compile time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One is a compile-time constant (the compiler knows the value of x), the other is a run-time constant. A switch case can only use compile-time constants. To use other kinds of values, just use if...else.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by Salem View Post
    > const int y = 2;
    This one is a const known at compile time.

    > const int y =(const int)( aXML->y() );
    This is a run-time initialisation. The 'const' comes in later, if you then try to assign something to y.

    case labels need to be known at compile time.
    How can I make a switch case with run-time variables then?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use an if-else chain. Certain associative containers might also be suitable depending on what exactly you are trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-30-2010, 09:26 AM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM