switch case with variables

This is a discussion on switch case with variables within the C++ Programming forums, part of the General Programming Boards category; Why the following code doesn't give errors when I try to compile with gcc, and.... Code: #include <stdio.h> int x ...

  1. #1
    frs
    frs is offline
    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 11:38 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    frs
    frs is offline
    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
    21,433
    You could use an if-else chain. Certain associative containers might also be suitable depending on what exactly you are trying to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:44 AM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09: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, 01:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21