Thread: Accessing Globals With Same Name

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    Accessing Globals With Same Name

    I want to ask how do we access the global variables if we know that the same name also occurs in the local function. So according to C local function with override the global but what if i want to access the global variable's value. Also the same problem occurs if i nest certain statements and use the same variable name throughout then how am i going to get the specific value as per level. eg.
    [Code]
    int a=11;
    int main()
    {
    int a=10;
    {
    int a=5;
    {
    int a=4;
    {
    int a=3;
    printf("%d",a);//How can i access other values of a from here
    }
    }
    }

    }

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    I don't have a clue but surely it would be easier just to give the variables different names?

    edit: you could do this if you really want to:
    Code:
    int foo = 1;
    int *pf = &foo;
    
    {
       int foo = 2;
       printf("%d, %d", foo, *pf);
    }
    Last edited by Brian; 07-21-2006 at 02:05 AM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I don't know for C, but in C++ you can use the scope resolution operator, by itself. C might actually have no implementation of an operator or function to do such a thing. I'm not the person to ask, though.
    Sent from my iPadŽ

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    There is no way. As Brian said, you need to use different variable names. Some people prepend global variables with g_, as in g_foo to differentiate them from local variables. Another method is to put your global variables in a static structure, so they are accessed globals.foo. Both these methods can make code more readable as it makes it immediately clear that a variable is a global.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    That would result in a kind of Systems Hungarian Notation, where the first letter of the variable name indicates the scope, the second the data type, then after an underscore comes the name of the variable.

    So here you'd have gi_foo (global integer called foo) and li_foo (local integer called foo).

    People have mixed feelings about this, as styles get mixed up and it can lead to more confusion, not less under certain circumstances.

    Even so, using the same name and trying to get a variable of the same name from a parent's scope (even if it was possible) can make for some pretty unreadable code!
    I think you can put a signature here.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Never thought of using a struct to hold globals, that is a good idea!

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Quote Originally Posted by SlyMaelstrom
    I don't know for C, but in C++ you can use the scope resolution operator, by itself. C might actually have no implementation of an operator or function to do such a thing. I'm not the person to ask, though.

    I recognize that in C++ there is a scope resolution operator but whenever it is called it only calls the global and doesn't pay heed to local nested variables. But is there a way to do it in C++, if yes then please clarify

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But is there a way to do it in C++, if yes then please clarify
    Yes, there is. Don't reuse identifiers from an enclosing scope. It's just that simple.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector : accessing values
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 06-01-2009, 09:26 PM
  2. Problems accessing logged on user via System.Security
    By conor20_ie in forum C# Programming
    Replies: 5
    Last Post: 11-16-2007, 07:52 AM
  3. problems accessing an external file
    By tony24tone in forum C Programming
    Replies: 2
    Last Post: 05-16-2004, 06:51 PM
  4. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 08:12 PM
  5. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 03:06 PM