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

Discussion in 'C Programming' started by Mohanasundaram, Jun 25, 2004.

  1. 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.
     
    Mohanasundaram, Jun 25, 2004
    #1
    1. Advertising

  2. Re: Accessing a global variable when there is a local variable inthe same name

    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.
     
    Martin Ambuhl, Jun 25, 2004
    #2
    1. Advertising

  3. Mohanasundaram

    James Hu Guest

    On 2004-06-25, 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?
    >


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

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

    return 0;
    }

    -- James
     
    James Hu, Jun 25, 2004
    #3
  4. Mohanasundaram

    Chris Dollin Guest

    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-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
     
    Chris Dollin, Jun 25, 2004
    #4
  5. "Mohanasundaram" <> wrote in message news:...
    > 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/
     
    Vijay Kumar R Zanvar, Jun 25, 2004
    #5
  6. Mohanasundaram

    CBFalconer Guest

    Re: Accessing a global variable when there is a local variable in thesame name

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 25, 2004
    #6
  7. Mohanasundaram

    Dan Pop Guest

    In <> (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:
     
    Dan Pop, Jun 25, 2004
    #7
  8. Re: Accessing a global variable when there is a local variable in the same name

    CBFalconer <> 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_Keith) <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.
     
    Keith Thompson, Jun 25, 2004
    #8
  9. In 'comp.lang.c', James Hu <> 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_pointer = &i;
    > int i = 20;
    >
    > /* ... use global_i_pointer 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/
     
    Emmanuel Delahaye, Jun 25, 2004
    #9
  10. (Dan Pop) writes:
    > In <>
    > (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_Keith) <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.
     
    Keith Thompson, Jun 25, 2004
    #10
  11. Mohanasundaram

    Dan Pop Guest

    In <> Keith Thompson <> writes:

    > (Dan Pop) writes:
    >> In <>
    >> (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.


    Which part of "This issue should NEVER arise in real C programs." was too
    difficult for you to understand?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 28, 2004
    #11
  12. (Dan Pop) writes:
    > In <> Keith Thompson <> writes:
    > > (Dan Pop) writes:
    > >> In <>
    > >> (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.

    >
    > Which part of "This issue should NEVER arise in real C programs." was too
    > difficult for you to understand?


    I understood it quite well, and I agree with it.

    You declared that "This issue should NEVER arise in real C programs."
    You could have stopped there, but you chose to present a solution.
    That's fine. I commented on your solution. I agreed that it would
    work, and raised another point that supports your contention that
    "This issue should NEVER arise in real C programs."

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 28, 2004
    #12
  13. Mohanasundaram

    Dan Pop Guest

    In <> Keith Thompson <> writes:

    > (Dan Pop) writes:
    >> In <> Keith Thompson <> writes:
    >> > (Dan Pop) writes:
    >> >> In <>
    >> >> (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.

    >>
    >> Which part of "This issue should NEVER arise in real C programs." was too
    >> difficult for you to understand?

    >
    >I understood it quite well, and I agree with it.
    >
    >You declared that "This issue should NEVER arise in real C programs."
    >You could have stopped there, but you chose to present a solution.


    A solution to a *homework question*, which you arbitrarily chose to treat
    as advice about how to write real C programs, otherwise your comment about
    future maintainers would be downright idiotic: homework answers have no
    future maintainers.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 29, 2004
    #13
  14. (Dan Pop) writes:
    > In <> Keith Thompson <> writes:

    [snip]
    > >I understood it quite well, and I agree with it.
    > >
    > >You declared that "This issue should NEVER arise in real C programs."
    > >You could have stopped there, but you chose to present a solution.

    >
    > A solution to a *homework question*, which you arbitrarily chose to treat
    > as advice about how to write real C programs, otherwise your comment about
    > future maintainers would be downright idiotic: homework answers have no
    > future maintainers.


    Who said it was a homework question? The OP didn't say so. It's
    entirely possible that it was homework; it's also possible that the OP
    was trying to find out whether C has some kind of scope resolution
    mechanism, as some other languages do.

    I didn't treat your solution as advice about how to write real C
    programs, but others might. In any case, I was simply expanding on
    what you wrote. I don't know why you have a problem with that.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 29, 2004
    #14
  15. Mohanasundaram

    Dan Pop Guest

    In <> Keith Thompson <> writes:

    > (Dan Pop) writes:
    >> In <> Keith Thompson <> writes:

    >[snip]
    >> >I understood it quite well, and I agree with it.
    >> >
    >> >You declared that "This issue should NEVER arise in real C programs."
    >> >You could have stopped there, but you chose to present a solution.

    >>
    >> A solution to a *homework question*, which you arbitrarily chose to treat
    >> as advice about how to write real C programs, otherwise your comment about
    >> future maintainers would be downright idiotic: homework answers have no
    >> future maintainers.

    >
    >Who said it was a homework question? The OP didn't say so. It's
    >entirely possible that it was homework;


    My own text, prefacing the actual code, made it crystal clear that it was
    supposed to be the answer to a stupid homework question and NOT used in
    real life C code:

    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.

    >it's also possible that the OP
    >was trying to find out whether C has some kind of scope resolution
    >mechanism, as some other languages do.


    And the answer is that it does (as shown in the various posted examples),
    but it's better left unused.

    >I didn't treat your solution as advice about how to write real C
    >programs, but others might.


    Why? Was my disclaimer clear enough for you, but not for others?

    >In any case, I was simply expanding on
    >what you wrote. I don't know why you have a problem with that.


    Because I hate idiotic expansions on what I write. As I have already
    explained, it makes no sense to invoke the future maintainers of something
    that is *explicitly* presented as "not to be done in real C programs".

    Without my explicit warning, your expansions would have been perfectly
    sensible.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 29, 2004
    #15
  16. (Dan Pop) writes:
    > In <> Keith Thompson <> writes:

    [...]
    > >In any case, I was simply expanding on
    > >what you wrote. I don't know why you have a problem with that.

    >
    > Because I hate idiotic expansions on what I write.


    Dan, it's really not that big a deal. What I wrote may not have been
    necessary; your overreaction to it certainly wasn't.

    In the future, I will spend less time worrying about your opinion
    other than on relevant technical issues. I'll probably continue to do
    things that make you angry (because there doesn't seem to be any way
    to avoid that other than leaving the newsgroup). You can deal with
    that in whatever way you choose.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jun 30, 2004
    #16
  17. Hi Dan,

    This was an interview question. It is not a homework question. I
    just wanted to know whether it is possible to do that like what we do
    in C++. We use :: operator in C++ to access the global variable. I
    wanted to know is there any language support in C to do this. If you
    know please share the answer. If not please ......

    I though the answer is either
    There is no language support in C
    or
    There is an operator or something similar to do so like ::

    Mohan.
     
    Mohanasundaram, Jul 1, 2004
    #17
  18. Hi Thomson,

    I feel you make lot of sense and show maturity in the full
    thread. You are right and I wanted to know whether C supports any
    thing similar to :: for accesing global varibles in C++. It was not a
    homework question. I decided to ask this in this group because I had
    and argument with an interviewer.

    Regards,
    Mohan.
     
    Mohanasundaram, Jul 1, 2004
    #18
  19. Mohanasundaram

    Dan Pop Guest

    In <> (Mohanasundaram) writes:

    >Hi Dan,
    >
    > This was an interview question. It is not a homework question. I
    >just wanted to know whether it is possible to do that like what we do
    >in C++. We use :: operator in C++ to access the global variable. I
    >wanted to know is there any language support in C to do this. If you
    >know please share the answer. If not please ......


    Are you a patent idiot or what? I have already provided the answer.
    Or are you unable to read standard C code?

    There was no mention of the C++ :: operator in your initial question,
    was it?

    >I though the answer is either
    >There is no language support in C
    >or
    >There is an operator or something similar to do so like ::


    And you were wrong. There is language support in C, but it doesn't
    involve any operator.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 1, 2004
    #19
  20. (Dan Pop) writes:
    > In <>
    > (Mohanasundaram) writes:
    > >Hi Dan,
    > >
    > > This was an interview question. It is not a homework question. I
    > >just wanted to know whether it is possible to do that like what we do
    > >in C++. We use :: operator in C++ to access the global variable. I
    > >wanted to know is there any language support in C to do this. If you
    > >know please share the answer. If not please ......

    >
    > Are you a patent idiot or what? I have already provided the answer.
    > Or are you unable to read standard C code?
    >
    > There was no mention of the C++ :: operator in your initial question,
    > was it?


    The intent of Mohanasundaram's original question was to ask whether C
    has an operator that provides access to declarations in outer scopes
    that are otherwise hidden. (That wasn't entirely clear from his
    original question, but it's very clear now that he's come back and
    clarified it.) There are such operators in some other languages (C++
    and Perl have "::", Ada has ".", and I'm sure there are plenty of
    other examples). The question might have been clearer if he had
    mentioned C++'s "::" operator, but this is, after all, a C newsgroup.

    The underlying question was actually a very good one, regardless of
    the fact that the answer happens to be "no". And I'd recommend to
    other posters, particularly newbies, that if you see us going off
    track in discussing something you've asked about, please jump in early
    and clarify what you really meant.

    > >I though the answer is either
    > >There is no language support in C
    > >or
    > >There is an operator or something similar to do so like ::

    >
    > And you were wrong. There is language support in C, but it doesn't
    > involve any operator.


    The solution Dan Pop provided was not so much a feature of C as a
    workaround for the lack of a feature in C. It was a reasonable answer
    to the original question as stated, but it didn't answer the intended
    question. Yes, as I said, the original question could have been
    worded more clearly; it took me a while to figure out the point
    myself, and I'm not criticizing Dan for missing it.

    I think most of the regulars here have either stopped reading Dan
    Pop's postings or have learned to apply a mental "Dan Pop filter",
    reading for technical content (which is usually very good) and
    ignoring the abuse.

    I hope that not too many newbies are put off by Dan Pop's appalling
    rudeness, which I expect he'll demonstrate again in response to this
    post.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Jul 1, 2004
    #20
    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. Shilpa

    Accessing global variable in a local block

    Shilpa, Nov 16, 2006, in forum: C Programming
    Replies:
    9
    Views:
    343
    Chris Torek
    Nov 16, 2006
  2. shilpa
    Replies:
    4
    Views:
    287
  3. Replies:
    2
    Views:
    266
    Thomas J. Gritzan
    Feb 13, 2007
  4. Bobby Chamness
    Replies:
    2
    Views:
    2,401
    Joe Smith
    Apr 22, 2007
  5. rubyrus
    Replies:
    5
    Views:
    112
Loading...

Share This Page