Afficher un message
Vieux 17/10/2007, 06h42   #3
Justin Spahr-Summers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Identifying variables: what process do you follow?

On Oct 17, 12:30 am, jaysome <jays...@hotmail.com> wrote:
> On Tue, 16 Oct 2007 20:24:26 -0000, Justin Spahr-Summers
> <Justin.SpahrSumm...@gmail.com> wrote:
> >On Oct 16, 9:23 am, Kevin Walzer <k...@codebykevin.com> wrote:
> >> With C, such an approach doesn't seem practical. You need to identify
> >> some variables ahead of time just to have a program that will compile!

>
> >C99 (which is the latest standard) allows mixed declarations and code,
> >but is not widely implemented in full; however, in C90, you can also
> >do something like the following:

>
> >int myfunction (void) {
> > int a = 5;
> > /* do stuff with 'a' here */
> > {
> > int b = 6;
> > /* do stuff with 'a' and 'b' here */
> > }

>
> > /* do more stuff with 'a' here */
> > return a;
> >}

>
> >The scope of the variable 'b' begins at the inner opening brace and
> >ends at the inner closing brace, and as long as you keep its
> >declaration at the very beginning of that scope, it's legal in both
> >C90 and C99. Any amount of nested braces is allowed as well.

>
> If the scope of the variable 'b' begins at the inner opening brace,
> shouldn't I be allowed to do something like this?
>
> int myfunction (void) {
> int a = 5;
> /* do stuff with 'a' here */
> {
> int b0 = b;
> int b = 6;
> /* do stuff with 'a' and 'b' here */
> }
>
> /* do more stuff with 'a' here */
> return a;
>
> }
>
> When I do this, I get a compiler error:
>
> error C2065: 'b' : undeclared identifier
>
> What gives?


I was thinking in a hurry after a long day. It obviously begins where
the declaration is. I'm fairly certain you realize that, but the OP
might not, so thanks for bringing it up.

  Réponse avec citation
 
Page generated in 0,05929 seconds with 9 queries