accessing global variables in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manju23reddy
    New Member
    • Aug 2008
    • 7

    accessing global variables in c

    consider the program
    int a = 10;
    int main()
    {
    int a = 20;
    }

    now i need to acess the global a within main so how to do it.


    but i have solution like this
    int main()
    {
    int i = a;
    int a = 20;
    }

    but is there any alternative.
  • Billiska
    New Member
    • Aug 2008
    • 13

    #2
    I think your second example should precisely be:
    Code:
    int a=10;
    int main()
    {
     int *pa=&a;
     int a=20;
    }
    Then you can use pa to access the global 'a'

    And here a have another way(maybe stupid but it works):
    Code:
    int a=10;
    int GetA() {return a;}
    int main()
    {
     int a=20;
    }
    With this, you can use GetA() to retrieve 'a'.
    I think this one will not work with 'interpreter'. Can anyone make sure of this? Thanks.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      The best solution is just to not re-use the name 'a'. In larger programs, you will want to give your variables descriptive names, such as 'sum' or 'rateOfInterest ' rather than 1-letter names. In this case, there's not usually a need to have two variables with the same name.

      If you are forced into this situation, I believe preceding the variable name with the scope resolution operator "::" will access the global variable.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by Ganon11
        The best solution is just to not re-use the name 'a'. In larger programs, you will want to give your variables descriptive names, such as 'sum' or 'rateOfInterest ' rather than 1-letter names. In this case, there's not usually a need to have two variables with the same name.

        If you are forced into this situation, I believe preceding the variable name with the scope resolution operator "::" will access the global variable.
        I believe the :: operator will work only for C++ and not for C

        Raghu

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by gpraghuram
          I believe the :: operator will work only for C++ and not for C
          I agree, it isn't uncommon in big projects for the coding standard to specify a prefix for global variable names g_ for instance.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Banfa
            I agree, it isn't uncommon in big projects for the coding standard to specify a prefix for global variable names g_ for instance.
            Yep, those are the last hiccups from that preposterous Hungarian whatchamacallit
            naming scheme that is heavily used by Microsoft. Ever so handy when the type
            or storage rights change for a variable: everything that uses the variable has to
            be changed then; hurray!

            kind regards,

            Jos

            Comment

            Working...