How to access the global

  • Thread starter Ganesh Kundapur
  • Start date
G

Ganesh Kundapur

Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?
 
M

Mike Wahler

Ganesh Kundapur said:
Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

As you have it, you cannot. But with a scope adjustment:

#include <stdio.h>

int x = 10;

int main(void)
{
{
int x = 5;
}

printf("%d\n", x); /* prints 10 */

return 0;
}

or a 'back door' access via a pointer:

#include <stdio.h>

int x = 10;

int main(void)
{
int *p = &x; /* must come *before* declaration of 'x' below */
int x = 5;

printf("%d\n", *p); /* prints 10 */
return 0;
}

Or more sensibly, change the name of one of the 'x's

Also, globals are generally not recommended without
a compelling reason to define them.

Is your question only academic, or is there a particular
problem you're trying to solve?

-Mike
 
D

dis

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

It would be more sensible to rename one of both variables, but one
possibility would be:

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

Nejat AYDIN

Ganesh said:
Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

int x = 10;

int read_x (void) { return x; }
void write_x (int a) { x = a; }

int main (void)
{
int x = 5;

write_x(0);
return read_x();
}
 
R

Richard Bos

Ganesh Kundapur said:
int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

You can't. You've explicitly taken the global object out of scope within
main(). If you need it, don't do that.

Richard
 
C

Chris Dollin

Ganesh said:
Hi,

int x = 10;
int main ()
{
int x = 5;
??? /* Access the global x here */
}

How to access the global x within main?

(a) Don't call the local in main `x` if you want to access a global
called `x`. [There are solutions that let you do this; they're
typically much more complciated and of less benefit, except for (b).]

(b) Don't call globals names like `x`, whether or not you want to
access them from inside functions with locals called `x`.

(c) Why are you asking?
 
K

Kevin Bracey

In message <[email protected]>
"dis said:
It would be more sensible to rename one of both variables, but one
possibility would be:

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

That totally failed to occur to me (and all the other respondents I've seen).
I'm not sure whether to complement you on your C prowess, or be worried what
sort of programmer would have such a scoping construct at their fingertips.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top