initialization in switch

T

Teddy

Hello all

consider the code fragment below:
case 1:
{
int i = 1;
break;
}

VC give this error:error C2360: initialization of 'i' is skipped by
'case' label

but if i write like this:
case 1:
{
int i;
i = 1;
break;
}

it is successfully compiled


why ?
 
C

Christopher

Teddy said:
Hello all

consider the code fragment below:
case 1:
{
int i = 1;
break;
}

VC give this error:error C2360: initialization of 'i' is skipped by
'case' label

but if i write like this:
case 1:
{
int i;
i = 1;
break;
}

it is successfully compiled


why ?

Well, what happens when somewhere down the line, after the switch, or even
in a differant case, the value of i is queried? e.g. x = i; Unless case 1 is
guarenteed then there is no such thing as i. Because case 1 is the only
block aware of i's existance at all. In the second code snipper there is
always an i. The compiler is smart enough to know this.
 
D

Donovan Rebbechi

Hello all

consider the code fragment below:
case 1:
{
int i = 1;
break;
}

Are you sure you did exactly the above, and not something like this:

switch (i) {
case 1:
int i = 1;
break;
case 2: // skips initialization of i, but i still has scope!
// is the variable i initialized or not ? it's in limbo state

This should give such an error, but the code you posted shouldn't.
VC give this error:error C2360: initialization of 'i' is skipped by
'case' label

but if i write like this:
case 1:
{
int i;
i = 1;
break;
}

it is successfully compiled

Because i is not initialized.

Cheers,
 
D

Donovan Rebbechi

Well, what happens when somewhere down the line, after the switch, or even
in a differant case, the value of i is queried? e.g. x = i;

In the code he posted, i has block scope. So there's no ambiguity -- if that
happens, it's an "undeclared identifier".
Unless case 1 is
guarenteed then there is no such thing as i.

What about this :

if (flag == 0) {
int i = 0;
} else if ( flag == 1) {
int j = 0;
} else if ( flag == 2) {
int k = 0;
}

if flag is set to 0, then there is no such thing as i either.
Because case 1 is the only
block aware of i's existance at all.

Likewise above: only the first block knows about i. Yet it is legal.
In the second code snipper there is
always an i. The compiler is smart enough to know this.

Are you saying that block scope is only allowed if the compiler knows
whether or not that block is executed ?

Cheers,
 
G

Gary Labowitz

Using what you gave as example, I compiled and excuted successfully with
VC++ 6.0 and MinGW 3.4.2
You will need to give fully failing program in order for checking it here.

#include <iostream>
#include <ostream>

int main( )
{
int j = 1;
switch (j)
{
case 1:
{
int i = 1;
break;
}
}
std::cout << j << std::endl;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top