• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: why can't you initialize variable in case statements
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >why can't you initialize variable in case statements (From: Bob Sabiston <email@hidden>)

  • Prev by Date: subclass variable not defined in scope?
  • Next by Date: subclass variable not defined in scope? (also)
  • Previous by thread: Re: why can't you initialize variable in case statements
  • Next by thread: Re: why can't you initialize variable in case statements
  • Index(es):
    • Date
    • Thread