473,511 Members | 12,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing the global variable inside a function when we have a variable of same name

Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable.

Example

Expand|Select|Wrap|Line Numbers
  1. #include<something.h>
  2. int countVal = 100;
  3. /*
  4. Some stuff
  5. */
  6.  
  7. void myFunction()
  8. {
  9. int countVal;
  10. }
  11.  
So here in function myFunction i have a local variable same as global variable.
So inside the function can i still use global variable(countVal). If yes how.?

Regards
Dheeraj Joshi
Jan 13 '10 #1
4 3701
Airslash
221 New Member
nameless namespace.

preceed your global variable with :: when calling it.

so use this code:

Expand|Select|Wrap|Line Numbers
  1. #include<something.h>
  2. int countVal = 100;
  3. /*
  4. Some stuff
  5. */
  6.  
  7. void myFunction()
  8. {
  9. int countVal;
  10. // to use global variable
  11. ::countVal
  12. // to use local variable
  13. countVal
  14. }
  15.  
Jan 13 '10 #2
Banfa
9,065 Recognized Expert Moderator Expert
Note the Scope Resolution Operator :: only exists in C++ so this technique doesn't work in C where you are basically stuffed.
Jan 13 '10 #3
donbock
2,426 Recognized Expert Top Contributor
You can be tricky like this:
Expand|Select|Wrap|Line Numbers
  1. int countVal = 100; 
  2. static int * const prwGlobalCountVal = &countVal;
  3. static const int * const proGlobalCountVal = &countVal;
  4. ...
  5. void myFunction() 
  6.    int countVal; 
  7.    // for read/write access to global variable 
  8.    ... *prwGlobalCountVal ...
  9.    // for read-only access to global variable 
  10.    ... *proGlobalCountVal ...
  11.    // to use local variable 
  12.    ... countVal ...
  13. }
But this is a good way to outsmart yourself. Much better to change the name of the local variable so there is no collision.

Notice that this trick creates one or two aliases of the global variable. Therefore, you must make a point of not associating the restrict type qualifier with the global variable.

A good lint will warn you if a local name overrides a wider-scope name.
Jan 13 '10 #4
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Ok... Thanks for the reply...

Regards
Dheeraj Joshi
Jan 15 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
102743
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
2
8803
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
2
2377
by: C Gillespie | last post by:
Dear All, I have 2 arrays var A1 = new Array(); A1 ="Y2"; var B1 = new Array(); B1 ="Y1"; B1 ="sink";
44
1973
by: Mohanasundaram | last post by:
int i = 10; int main() { int i = 20; return 0; } Hi All, I want to access the global variable i inside the main. Is there
7
3108
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
9
356
by: Shilpa | last post by:
Hi, I just wanted to know whether we can access global variable within a local block , where both variables are having same name. For ex: int temp=5 ; { int temp=10;
12
3089
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
1
29312
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
4
5977
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
0
7251
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
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7089
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...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.