CBFalconer <cb********@yah oo.com> writes:
Chris Dollin wrote: Mohanasundaram wrote:
int i = 10;
int main()
{
int i = 20;
return 0;
}
I want to access the global variable i inside the main. Is there
a way to do this in C?
There's the easy way and the smartarse way.
... snip ...
Around here, for a question like this, only the smartarse way is
acceptable.
The obvious way, as others have mentioned, is to change the name of
the local variable. (Some languages have ways to refer directly to
variables in outer scopes using expanded names; C doesn't. I suspect
that's what the OP was really asking about.)
But the smartarse way, using a pointer, does illustrate what might
sometimes be a relevant point: just because a function doesn't have
direct visibility to a variable (either because it's hidden by a
declaration in an inner scope, or because it's a static variable
declared in another file or function), you can't necessarily assume
that the function can't read or modify the variable.
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.