473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Scope resolution in C

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
6 1837
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: exits funnel | last post by:
Hello, I'm confused by the use of the Scope resolution operator on the indicated lines in the following code (which was copied from Thinking in C++ by Bruce Eckel). Removing them seems to have...
0
by: Howard Gardner | last post by:
/* I don't have to provide scope resolution in sit 0 below: adl finds the function that I mean to use. I do have to provide scope resolution in sits 1 and 2. Can anyone teach me a trick to...
3
by: richard pickworth | last post by:
Hello :) I am familiar with using scope resolution operator to define classes, but why does it turn up elsewhere? thanks richard
16
by: sushant | last post by:
hi , can we use scope resolution operator (::) in C??? sushant
6
by: Jack | last post by:
I want to write code that can be compiled as C and C++. In the Windows API, there are many macros that help with scope resolution. For example: #ifdef __cplusplus #define SNDMSG ::SendMessage...
5
by: Shan | last post by:
In the following code: func() { int SmeVar; { int SmeVar; //Here I want to use SmeVar defined in the outer block how can I do that ? }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.