Re: Scope resolution in C



On 30 Dec 2006 08:09:01 -0800, "Abhi" <abhisheksgumadi@xxxxxxxxx>
wrote:

Referring to the post given by MR herald,

Which you should have quoted to provide some context.

If the statements

Small nit. The two lines of code below are not statements but
declarations (that also serve as definitions).


int *ap = &a;
static int a = 10;

are reversed, wont the results for a be the same.. I think this answer

For each a - yes. But not for ap.

should be given independent of the order in which the declarations are

But the standard states that the scope of an identifier begins just
after the declaration. Therefore order is important.

In the code as posted, at the time the pointer is initialized, the
only a in scope is the one (not shown) declared at file scope. This a
is not hidden by the a at block scope until the scope of that
identifier begins. Therefore, the only value available to be assigned
to ap is the address of the a at file scope.

However, if you reverse the declarations, by the time you declare the
pointer, the scope of the block a will have begun (resulting in the a
at file scope being hidden). Therefore, the only value available to
be assigned to ap is the address of the a at block scope.

done and I think the right way of doing it is by using the function to
access it as global.


Remove del for email
.



Relevant Pages

  • Re: c99/c++ localised variable definition
    ... When variables are used only in a limited scope I find it better to ... Forcing variable declarations to the top of the function means extra ... > reason why things like ancillary counter variables seem reasonable, ... you can combine the decl and the initialization. ...
    (freebsd-arch)
  • Re: Making C better (by borrowing from C++)
    ... there's no way to be certain unless declarations are restricted not ... merely to the top of the scope, but to the top of the function. ... line by line for the matching declaration; the context switch between ... the two search modes costs at least as much as the nominally more ...
    (comp.lang.c)
  • Re: Identify new pairs in an array
    ... Leaving us to figure it out for ourselves from the code? ... [declarations below rearranged into groups I can comment on] ... One-character variable names suck even more when they ... You should limit the scope of variables to the smallest possible scope. ...
    (comp.lang.perl.misc)
  • Re: About String
    ... My idea is that every statement list is a scope. ... What is so special in statement lists? ... and to me declarations intermixed with statements look similarly ... You declare something inside a loop body, ...
    (comp.lang.ada)
  • Re: Java needs "goto" (was Re: hi)
    ... declarations are collected at the start of the method, ... the whole thing and starting from scratch. ... Too much work to be done and too lil time to do it. ... Played with this scope thing in just about ...
    (comp.lang.java.programmer)