Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to access the global

Reply
Thread Tools

How to access the global

 
 
Ganesh Kundapur
Guest
Posts: n/a
 
      10-07-2003
Hi,

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

How to access the global x within main?

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-07-2003
"Ganesh Kundapur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
dis
Guest
Posts: n/a
 
      10-07-2003
"Ganesh Kundapur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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;
}



 
Reply With Quote
 
Nejat AYDIN
Guest
Posts: n/a
 
      10-07-2003
Ganesh Kundapur wrote:
>
> 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();
}
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      10-07-2003
Ganesh Kundapur <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-07-2003
Ganesh Kundapur wrote:

> 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?

--
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
 
Reply With Quote
 
Kevin Bracey
Guest
Posts: n/a
 
      10-07-2003
In message <bltqls$cfq$(E-Mail Removed)1.nb.home.nl>
"dis" <(E-Mail Removed)> wrote:

> 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.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Global variables on par with ASP's global.asa Wayne ASP .Net 2 11-11-2003 10:58 PM
Global Error handling in Applicatio_Error() of Global.asax VSK ASP .Net 1 07-29-2003 03:12 AM



Advertisments