Re: why can't you initialize variable in case statements
Re: why can't you initialize variable in case statements
- Subject: Re: why can't you initialize variable in case statements
- From: "Corey O'Connor" <email@hidden>
- Date: Wed, 21 Sep 2005 14:01:02 -0700
On 9/21/05, Bob Sabiston <email@hidden> wrote:
> Hello,
>
> In trying to port to xcode, I get errors when a variable is initialized
> after any goto statement or within a case statement. Why? Is that a
> problem?
>
> error: jump to case label
> error: crosses initialization of 'int handled'
>
> Thanks
> Bob
I don't know about goto's, but I've run into something similar for
switch statements.
If you have something like this:
switch(theCase)
{
case 1:
int foo = 0;
// Do stuff
break;
case 2:
foo = 1;
// Do other stuff.
break;
}
if(foo == 0)
{
// Whatever
}
Then yes, that is a problem. If you just have something like this:
switch(theCase)
{
case 1:
int foo = 0;
// Do stuff
break;
case 2:
// No foo!
// Do other stuff.
break;
}
// Note: No use of foo outside "case 1"!
Then that's OK, but should be written:
switch(theCase)
{
case 1:
{
int foo = 0;
// Do stuff
}
break;
case 2:
// Do other stuff.
break;
}
Hope that helps.
--
-Corey O'Connor
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden