Declare a variable in a switch

This is a discussion on Declare a variable in a switch within the C++ Programming forums, part of the General Programming Boards category; This is more of a question, but why is this not allowed? Code: switch ( choice ) { case 1: ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Declare a variable in a switch

    This is more of a question, but why is this not allowed?

    Code:
    switch ( choice )
    {
    case 1:
       int x = 5;
       break;
    }
    You would get somthing like:

    main.cpp(85) : error C2360: initialization of 'x' is skipped by 'case' label
    In some cases, decalring a new variable within a case structure like the above would erradicate having to delcare the value at the start of the program.
    Is there any reason why the language does not allow it?
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Although I don't know why it isn't possible, there's an easy workaround to this.
    This is what I do:

    Code:
    switch (choice)
    {
        case 1:
        {
            int x = 5;
            break;
        }
    }
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can declare a new variable before any case statements:
    Code:
    int main() {
        
        int choice;
        choice = 2;
        switch (choice) {
               int x;
               case 1:
                    x=5;
                    break;
               case 2:
                    choice++;
               break;
        }
        return 0;
    }
    Edit: otherwise you get the interesting case where the variable x is in scope in case 2, but has not been declared and the initializer has therefore been skipped.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Thanks guys.

    I was unaware of those work-arounds.
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is not allowed because otherwise the scope of x would last until the closing brace of the switch. That is, x would be in scope under other case labels too, but it would be possible to get to them with x being defined.

    Same thing with goto statement which can't jump over variable definitions.
    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).

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by swgh View Post
    Is there any reason why the language does not allow it?
    Imagine:

    Code:
    switch(x)
    {
        case 1:
            SomeClass foo;
            break;
        case 2:
            foo.doSomething();
            break;
    }
    If you hit case 2, you use an object which hasn't been constructed. Boom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global and static variable in a class delivered in a DLL
    By George2 in forum C++ Programming
    Replies: 16
    Last Post: 04-13-2008, 09:19 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  4. declare a variable in a case/switch?
    By Oluf in forum Windows Programming
    Replies: 2
    Last Post: 12-13-2003, 01:39 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 04:51 AM

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