472,799 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Dheeraj Joshi
1,123 Expert 1GB
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 3650
Airslash
221 100+
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 Expert Mod 8TB
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 Expert 2GB
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 Expert 1GB
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
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
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
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
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
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
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
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
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
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 = "" ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.