Scope resolution in C

S

Subra

Hi,

If I have the same varible defined in global as well as in local
scope, how to access global scope varible in a function having the same
local def.

#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a);
}
#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",::a); // How to access the global def??
}
 
M

Mark McIntyre

Hi,

If I have the same varible defined in global as well as in local
scope, how to access global scope varible in a function having the same
local def.

You can't.

Having two identically named variables with different scope is a
design error. To fix it, rename one of the variables.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jacob navia

Subra a écrit :
Hi,

If I have the same varible defined in global as well as in local
scope, how to access global scope varible in a function having the same
local def.

#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",a);
}
#include<stdio.h>
static int a=25;
int main()
{
static int a = 10;
printf("%d",::a); // How to access the global def??
}

You can't do this. The scope rules forbid accessing
a global variable if there is a local (or in an inner scope)
variable of the same name.

Some compilers will warn you if by accident you do this.

For instance if using lcc-win32 just set the option -A
at the command line.

Shadowing of variables can be the source of very difficult bugs,
and in general it is better to pass your code with such a compiler or
with another tool to be sure there isn't a problem like this.
 
C

CBFalconer

Mark said:
You can't.

Having two identically named variables with different scope is a
design error. To fix it, rename one of the variables.

It's not a design error. It is a valid technique for protecting
the outer scope variable from modification (or access) within the
inner scope.

--
Some informative links:
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
M

Mark McIntyre

It's not a design error.

Er? Its a design error if you need to access the outer scope variable.
It is a valid technique for protecting
the outer scope variable from modification (or access) within the
inner scope.

Sure, but hte OP wanted to do the reverse.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
A

Abhi

Referring to the post given by MR herald,
If the statements

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

are reversed, wont the results for a be the same.. I think this answer
should be given independent of the order in which the declarations are
done and I think the right way of doing it is by using the function to
access it as global.
With Regards,
Abhishek S
 
B

Barry Schwarz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top