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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mohanasundaram

    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.
  • Martin Ambuhl

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

    Mohanasundaram wrote:
    [color=blue]
    > 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?[/color]

    By not masking it with a redeclaration inside main.

    Comment

    • James Hu

      #3
      Re: Accessing a global variable when there is a local variable in the same name

      On 2004-06-25, Mohanasundaram <mohanasundaram @msn.com> wrote:[color=blue]
      > 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?
      >[/color]

      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

      Comment

      • Chris Dollin

        #4
        Re: Accessing a global variable when there is a local variable in the same name

        Mohanasundaram wrote:
        [color=blue]
        > 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?[/color]

        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

        Comment

        • Vijay Kumar R Zanvar

          #5
          Re: Accessing a global variable when there is a local variable in the same name


          "Mohanasundaram " <mohanasundaram @msn.com> wrote in message news:9bb9619e.0 406242156.73cf7 e4@posting.goog le.com...[color=blue]
          > 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.[/color]

          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/


          Comment

          • CBFalconer

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

            Chris Dollin wrote:[color=blue]
            > Mohanasundaram wrote:
            >[color=green]
            >> 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?[/color]
            >
            > There's the easy way and the smartarse way.[/color]
            .... snip ...

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

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

            Comment

            • Dan Pop

              #7
              Re: Accessing a global variable when there is a local variable in the same name

              In <9bb9619e.04062 42156.73cf7e4@p osting.google.c om> mohanasundaram@ msn.com (Mohanasundaram ) writes:
              [color=blue]
              >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?[/color]

              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@ifh.de

              Comment

              • Keith Thompson

                #8
                Re: Accessing a global variable when there is a local variable in the same name

                CBFalconer <cbfalconer@yah oo.com> writes:[color=blue]
                > Chris Dollin wrote:[color=green]
                > > Mohanasundaram wrote:
                > >[color=darkred]
                > >> 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?[/color]
                > >
                > > There's the easy way and the smartarse way.[/color]
                > ... snip ...
                >
                > Around here, for a question like this, only the smartarse way is
                > acceptable.[/color]

                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) kst-u@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.

                Comment

                • Emmanuel Delahaye

                  #9
                  Re: Accessing a global variable when there is a local variable in the same name

                  In 'comp.lang.c', James Hu <jxh@despammed. com> wrote:
                  [color=blue][color=green]
                  >> 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?
                  >>[/color]
                  >
                  > 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;
                  > }[/color]

                  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/

                  Comment

                  • Keith Thompson

                    #10
                    Re: Accessing a global variable when there is a local variable in the same name

                    Dan.Pop@cern.ch (Dan Pop) writes:[color=blue]
                    > In <9bb9619e.04062 42156.73cf7e4@p osting.google.c om>
                    > mohanasundaram@ msn.com (Mohanasundaram ) writes:[color=green]
                    > >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?[/color]
                    >
                    > 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.[/color]

                    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) kst-u@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.

                    Comment

                    • Dan Pop

                      #11
                      Re: Accessing a global variable when there is a local variable in the same name

                      In <lny8mb70uv.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                      [color=blue]
                      >Dan.Pop@cern.c h (Dan Pop) writes:[color=green]
                      >> In <9bb9619e.04062 42156.73cf7e4@p osting.google.c om>
                      >> mohanasundaram@ msn.com (Mohanasundaram ) writes:[color=darkred]
                      >> >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?[/color]
                      >>
                      >> Explain why you can't rename one of the two variables.
                      >>
                      >> If this is a homework question, try finding a better instructor. This[/color][/color]
                      ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^[color=blue][color=green]
                      >> issue should NEVER arise in real C programs.[/color][/color]
                      ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^[color=blue][color=green]
                      >>
                      >> #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.[/color]
                      >
                      >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.[/color]

                      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@ifh.de

                      Comment

                      • Keith Thompson

                        #12
                        Re: Accessing a global variable when there is a local variable in the same name

                        Dan.Pop@cern.ch (Dan Pop) writes:[color=blue]
                        > In <lny8mb70uv.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:[color=green]
                        > >Dan.Pop@cern.c h (Dan Pop) writes:[color=darkred]
                        > >> In <9bb9619e.04062 42156.73cf7e4@p osting.google.c om>
                        > >> mohanasundaram@ 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[/color][/color]
                        > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^[color=green][color=darkred]
                        > >> issue should NEVER arise in real C programs.[/color][/color]
                        > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^[color=green][color=darkred]
                        > >>
                        > >> #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.[/color]
                        > >
                        > >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.[/color]
                        >
                        > Which part of "This issue should NEVER arise in real C programs." was too
                        > difficult for you to understand?[/color]

                        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_Keit h) kst-u@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.

                        Comment

                        • Dan Pop

                          #13
                          Re: Accessing a global variable when there is a local variable in the same name

                          In <lnlli77bm3.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                          [color=blue]
                          >Dan.Pop@cern.c h (Dan Pop) writes:[color=green]
                          >> In <lny8mb70uv.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:[color=darkred]
                          >> >Dan.Pop@cern.c h (Dan Pop) writes:
                          >> >> In <9bb9619e.04062 42156.73cf7e4@p osting.google.c om>
                          >> >> mohanasundaram@ 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[/color]
                          >> ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^[color=darkred]
                          >> >> issue should NEVER arise in real C programs.[/color]
                          >> ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^[color=darkred]
                          >> >>
                          >> >> #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.[/color]
                          >>
                          >> Which part of "This issue should NEVER arise in real C programs." was too
                          >> difficult for you to understand?[/color]
                          >
                          >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.[/color]

                          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@ifh.de

                          Comment

                          • Keith Thompson

                            #14
                            Re: Accessing a global variable when there is a local variable in the same name

                            Dan.Pop@cern.ch (Dan Pop) writes:[color=blue]
                            > In <lnlli77bm3.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:[/color]
                            [snip][color=blue][color=green]
                            > >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.[/color]
                            >
                            > 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.[/color]

                            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_Keit h) kst-u@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.

                            Comment

                            • Dan Pop

                              #15
                              Re: Accessing a global variable when there is a local variable in the same name

                              In <lnlli6cnfz.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:
                              [color=blue]
                              >Dan.Pop@cern.c h (Dan Pop) writes:[color=green]
                              >> In <lnlli77bm3.fsf @nuthaus.mib.or g> Keith Thompson <kst-u@mib.org> writes:[/color]
                              >[snip][color=green][color=darkred]
                              >> >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.[/color]
                              >>
                              >> 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.[/color]
                              >
                              >Who said it was a homework question? The OP didn't say so. It's
                              >entirely possible that it was homework;[/color]

                              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.
                              [color=blue]
                              >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.[/color]

                              And the answer is that it does (as shown in the various posted examples),
                              but it's better left unused.
                              [color=blue]
                              >I didn't treat your solution as advice about how to write real C
                              >programs, but others might.[/color]

                              Why? Was my disclaimer clear enough for you, but not for others?
                              [color=blue]
                              >In any case, I was simply expanding on
                              >what you wrote. I don't know why you have a problem with that.[/color]

                              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@ifh.de

                              Comment

                              Working...