Connecting Tech Pros Worldwide Forums | Help | Site Map

initialization in switch

Teddy
Guest
 
Posts: n/a
#1: Jul 23 '05
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 ?


Christopher
Guest
 
Posts: n/a
#2: Jul 23 '05

re: initialization in switch



"Teddy" <duanduan12@hotmail.com> wrote in message
news:1117416598.164487.151050@g44g2000cwa.googlegr oups.com...[color=blue]
> 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 ?[/color]

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.




Donovan Rebbechi
Guest
 
Posts: n/a
#3: Jul 23 '05

re: initialization in switch


On 2005-05-30, Teddy <duanduan12@hotmail.com> wrote:[color=blue]
> Hello all
>
> consider the code fragment below:
> case 1:
> {
> int i = 1;
> break;
> }[/color]

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.
[color=blue]
> 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[/color]

Because i is not initialized.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Donovan Rebbechi
Guest
 
Posts: n/a
#4: Jul 23 '05

re: initialization in switch


On 2005-05-30, Christopher <anon@nospam.net> wrote:
[color=blue]
> 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;[/color]

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

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.
[color=blue]
> Because case 1 is the only
> block aware of i's existance at all.[/color]

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

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

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Gary Labowitz
Guest
 
Posts: n/a
#5: Jul 23 '05

re: initialization in switch


"Christopher" <anon@nospam.net> wrote in message
news:mIume.23756$6g3.5385@tornado.texas.rr.com...[color=blue]
>
> "Teddy" <duanduan12@hotmail.com> wrote in message
> news:1117416598.164487.151050@g44g2000cwa.googlegr oups.com...[color=green]
> > 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 ?[/color][/color]

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;
}

--
Gary


Teddy
Guest
 
Posts: n/a
#6: Jul 23 '05

re: initialization in switch


It was my fault
I posted the wrong code

Closed Thread