473,587 Members | 2,496 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(countV al). If yes how.?

Regards
Dheeraj Joshi
Jan 13 '10 #1
4 3710
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
102745
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> <instructions> def bar: <instructions>
2
8813
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 { public: Book()
2
2386
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
1997
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
3119
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 couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
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
3102
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 only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
1
29333
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
4
5986
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 = "" Then I create a class, some methods etc. In one of the methods I assign
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
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 we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.