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: Paul Walmsley <email@hidden>
- Date: Wed, 21 Sep 2005 22:33:10 +0100
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'/
I think the problem is due to the scoping and destruction of objects --
I guess that you're trying to do something like this:
switch (x) {
case 0:
int localVar=3;
status=localVar;
break;
case 1:
...
}
The solution is to ensure that any variables you create inside the case
are correctly destroyed by enclosing within an extra set of braces:
case 0: { // note brace
int localVar=3;
status=localVar;
break;
} // note brace
I think this is an ANSI requirement -- I know that Visual Studio has
required you to do it this way for a few years. I thought Codewarrior
had too.
_______________________________________________
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