How to access the global

Discussion in 'C Programming' started by Ganesh Kundapur, Oct 7, 2003.

  1. Hi,

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

    How to access the global x within main?
    Ganesh Kundapur, Oct 7, 2003
    #1
    1. Advertising

  2. Ganesh Kundapur

    Mike Wahler Guest

    "Ganesh Kundapur" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 7, 2003
    #2
    1. Advertising

  3. Ganesh Kundapur

    dis Guest

    "Ganesh Kundapur" <> wrote in message
    news:...

    > 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;
    }
    dis, Oct 7, 2003
    #3
  4. Ganesh Kundapur

    Nejat AYDIN Guest

    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();
    }
    Nejat AYDIN, Oct 7, 2003
    #4
  5. Ganesh Kundapur

    Richard Bos Guest

    Ganesh Kundapur <> 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
    Richard Bos, Oct 7, 2003
    #5
  6. Ganesh Kundapur

    Chris Dollin Guest

    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-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Oct 7, 2003
    #6
  7. Ganesh Kundapur

    Kevin Bracey Guest

    In message <bltqls$cfq$1.nb.home.nl>
    "dis" <> 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/
    Kevin Bracey, Oct 7, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    766
    Alvin Bruney
    Jul 29, 2003
  2. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  3. Daniel Bass
    Replies:
    2
    Views:
    6,856
    Daniel Bass
    Dec 31, 2003
  4. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,794
  5. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    798
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
Loading...

Share This Page