Defining variables in switch

Hi there!

I'd like to define a variable in switch:

1
2
3
4
5
6
7
8
9
switch(a)
{
case 1:
int b = 444;   //!
break;
case 2:
int c = 777;   //!
break;
}


but I can't. I get following errors:

crosses initialization of `int b'
crosses initialization of `int c'


Why is that ? :/
You can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
switch(a)
{
  case 1:
  {
    int b = 444;
    break;
  }
  case 2:
  {
    int c = 777; 
    break;
  }
}

Last edited on
Why is local scope a solution ?
closed account (z05DSL3A)
Both the examples are local scope, however, in the first example the variables are not declared in a manor that would not make them available to all parts of the code block and is therefor not allowed.
What's the 'manor' ?
manor → manner
I get it now - thx :)
Topic archived. No new replies allowed.