• 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: Multiple variable rules in for() statement
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multiple variable rules in for() statement


  • Subject: Re: Multiple variable rules in for() statement
  • From: David Ross <email@hidden>
  • Date: Thu, 12 Nov 2009 14:50:00 -0500

Jonathon,

GCC does not like declaring variables in a for statement. If you want to make the variable m have the scope for only the for loop, you would need:

foo()
{
	...
	{
		int i, m;
		for (i=0, m=0; i<5; i++){...}
		printf("Final value of i: %d\n",i);
	}
	...
}

If you don't mind the 'm' variable having the scope of its enclosing block, then you can omit the outer braces around the for.

— David

On Nov 12, 2009, at 2:29 PM, Jonathon Kuo wrote:

> I can't chance upon the right incantation for using both an existing variable and an inline new one in a for loop. I've boiled this down to a trivial show case, same results for both gcc 4.0.1 and 4.2.1:
>
>  int i;
>   . . .
>  for (i=0, int m=0; i<5; i++) { . . . };
>  printf("Final value of i: %d\n",i);
>
> error: syntax error before ‘int’
> error: syntax error before ‘)’ token
>
> So I tried this:
>
>  int i;
>   . . .
>  for (int m=0,i=0; i<5; i++) { . . . };
>  printf("Final value of i: %d\n",i);
>
> error: redefinition of ‘i’
>
> Is there a rule that ALL initialized loop variables are either new and local to the loop, or that none are? Surely Im doing something wrong here!
>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Multiple variable rules in for() statement
      • From: Clark Cox <email@hidden>
    • Re: Multiple variable rules in for() statement
      • From: Andrew Farmer <email@hidden>
References: 
 >Multiple variable rules in for() statement (From: Jonathon Kuo <email@hidden>)

  • Prev by Date: NSColor colorUsingColorSpace: extremely slow on 10.6?
  • Next by Date: Interested in a Tampa Bay Area CocoaHeads?
  • Previous by thread: Re: Multiple variable rules in for() statement
  • Next by thread: Re: Multiple variable rules in for() statement
  • Index(es):
    • Date
    • Thread