473,793 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing a global variable when there is a local variable in the same name

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
a way to do this in C?

Regards,
Mohan.
Nov 14 '05 #1
44 2040
Mohanasundaram wrote:
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
a way to do this in C?


By not masking it with a redeclaration inside main.
Nov 14 '05 #2
On 2004-06-25, Mohanasundaram <mo************ @msn.com> wrote:
int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?


int i = 10;
int main(void)
{
int *global_i_point er = &i;
int i = 20;

/* ... use global_i_pointe r to access the global variable i
from inside main ... */

return 0;
}

-- James
Nov 14 '05 #3
Mohanasundaram wrote:
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
a way to do this in C?


There's the easy way and the smartarse way.

The easy way is to rename the local i to something else, or to rename
the global i to something more sensible (a global variable called "i"
is probably a very bad idea).

The smararse way involves using pointers or nested extern declarations.
I'm not going to tell you the details. Just rename one or both i's.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #4

"Mohanasundaram " <mo************ @msn.com> wrote in message news:9b******** *************** **@posting.goog le.com...
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
a way to do this in C?

Regards,
Mohan.


int i = 10;
int main(void)
{
int i = 5;
{
extern int i;
i; /* the expression i will evaluate to 10 */
}
return 0;
}

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05 #5
Chris Dollin wrote:
Mohanasundaram wrote:
int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?


There's the easy way and the smartarse way.

.... snip ...

Around here, for a question like this, only the smartarse way is
acceptable.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #6
In <9b************ *************@p osting.google.c om> mo************@ msn.com (Mohanasundaram ) writes:
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
a way to do this in C?


Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.

#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
CBFalconer <cb********@yah oo.com> writes:
Chris Dollin wrote:
Mohanasundaram wrote:
int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?


There's the easy way and the smartarse way.

... snip ...

Around here, for a question like this, only the smartarse way is
acceptable.


The obvious way, as others have mentioned, is to change the name of
the local variable. (Some languages have ways to refer directly to
variables in outer scopes using expanded names; C doesn't. I suspect
that's what the OP was really asking about.)

But the smartarse way, using a pointer, does illustrate what might
sometimes be a relevant point: just because a function doesn't have
direct visibility to a variable (either because it's hidden by a
declaration in an inner scope, or because it's a static variable
declared in another file or function), you can't necessarily assume
that the function can't read or modify the variable.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
In 'comp.lang.c', James Hu <jx*@despammed. com> wrote:
int i = 10;
int main()
{
int i = 20;
return 0;
}

I want to access the global variable i inside the main. Is there
a way to do this in C?


int i = 10;
int main(void)
{
int *global_i_point er = &i;
int i = 20;

/* ... use global_i_pointe r to access the global variable i
from inside main ... */

return 0;
}


Clever. I'll try to keep it in mind.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #9
Da*****@cern.ch (Dan Pop) writes:
In <9b************ *************@p osting.google.c om>
mo************@ msn.com (Mohanasundaram ) writes:
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
a way to do this in C?


Explain why you can't rename one of the two variables.

If this is a homework question, try finding a better instructor. This
issue should NEVER arise in real C programs.

#include <stdio.h>

int i = 10;

int main()
{
int j = i;
int i = 20;
printf("%d\n", j);
return 0;
}

If the value of the global i might change during the execution of main(),
replace the definition of j by

int *p = &i;

but keep it still *before* the definition of the local i, and use *p any
time you need to access the global i.


Yes, that will work. It will also create a potential headache for
future maintainers. Code that silently changes behavior when the
order of declarations is changed is not generally a good idea.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
5155
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from the days before PHP got object-oriented features. For instance we currently have:
6
2109
by: Fernando Rodríguez | last post by:
Hi, I haven't used Python in quite some time, and I'm bit puzzled by this: counter = 0 class Blah(object): def run(self): counter += 1
2
2395
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";
11
2571
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
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;
9
3035
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29384
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 condition. The opinions expressed in this article are those of the author alone although many have...
112
5485
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
4
3722
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
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 #include<something.h> int countVal = 100; /* Some stuff */
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6777
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
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
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.