• 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: "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

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

  • Prev by Date: Re: Help porting from codewarrior?
  • Next by Date: subclass variable not defined in scope?
  • Previous by thread: 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