By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

Scope resolution in C

P: n/a
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??
}

Dec 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 28 Dec 2006 23:21:21 -0800, in comp.lang.c , "Subra"
<ma*********@gmail.comwrote:
>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
Dec 29 '06 #2

P: n/a
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.
Dec 29 '06 #3

P: n/a
Mark McIntyre wrote:
"Subra" <ma*********@gmail.comwrote:
>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.
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/>

Dec 29 '06 #4

P: n/a
On Fri, 29 Dec 2006 06:51:05 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.comwrote:
>Mark McIntyre wrote:
>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.
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
Dec 29 '06 #5

P: n/a
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

Mark McIntyre wrote:
On Fri, 29 Dec 2006 06:51:05 -0500, in comp.lang.c , CBFalconer
<cb********@yahoo.comwrote:
Mark McIntyre wrote:
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.

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
Dec 30 '06 #6

P: n/a
On 30 Dec 2006 08:09:01 -0800, "Abhi" <ab*************@gmail.com>
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
Dec 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.