Thread: What are memory address of global variables

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    What are memory address of global variables

    How to know the address of global variables

    Code:
    #include<stdio.h> 
        int a = 1;
       
        int b = 2;
     
    int main()
    {
        int a = 10;
       
        int b = 20;
        
        printf("a = %d \n", a);
        printf("b = %d \n", b);
    
    
           printf("Location of a = %p \n", &a);
        printf("Location of b = %p \n", &b);
        
        return 0;
    }
    a = 10
    b = 20
    Location of a = 0061FF2C
    Location of b = 0061FF28
    Program print the address of local variables

    Program show the memory location of local as well as global variable

    Code:
    #include<stdio.h>
     
        int x = 1;
       
        int y = 2;
     
    int main()
    {
        int a = 10;
       
        int b = 20;
        
        printf("x = %d \n", x);
        printf("y = %d \n", y);
        
        printf("a = %d \n", a);
        printf("b = %d \n", b);
    
    
           printf("a = %p \n", &a);
        printf("b = %p \n", &b);
        
        printf("x = %p \n", &x);
        printf("p = %p \n", &y);
        return 0;
    }
    x = 1
    y = 2
    a = 10
    b = 20
    a = 0061FF2C
    b = 0061FF28
    x = 00404004
    y = 00404008

    What happen if we declare global and local variable with same name and where does global variable store and how can we get their address
    Code:
    #include<stdio.h>
     
        int a = 1;
       
        int b = 2;
     
    int main()
    {
        int a = 10;
       
        int b = 20;
        
        printf("x = %d \n", x);
        printf("y = %d \n", y);
        
        printf("a = %d \n", a);
        printf("b = %d \n", b);
    
    
           printf("a = %p \n", &a);
        printf("b = %p \n", &b);
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    > What happen if we declare global and local variable with same name and where does global variable store and how can we get their address

    A Google search happens. And if not that, buying a Reference Book happens and going through it step by step happens. When you don't understand something other than the very basics of the programming foundation, asking a question happens.

    Use the scope resolution operator (::) to access global scope variables with same name as local scopes.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Zeus_ View Post
    >
    A Google search happens. And if not that, buying a Reference Book happens and going through it step by step happens. When you don't understand something other than the very basics of the programming foundation, asking a question happens.

    Use the scope resolution operator (: to access global scope variables with same name as local scopes.
    Thanks for your reply and my apologizes for not understanding response on the question. That question came in mind so I asked on the forum. I did google before posting question I wanted to know what happens when we declare global and local variable with same name.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by Zeus_ View Post
    Use the scope resolution operator ( :: ) to access global scope variables with same name as local scopes.
    The scope resolution operator is only for C++ not C. There may be compilers that allow this as an extension, but not gcc using -std=c18. I have not experimented with any other options.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    @gajya

    In C, a local variable of the same name as a Global variable, will hide the Global.

    Simple solutions:

    1) Don't use global variables if at all possible! Define them in main() (Or some other called function), then pass them as a value or as a pointer to other functions.

    2) Don't define a Global variable and a Local variable with the same name!!!

    If you want to display the address of a variable, you should cast them to a (void *) in printf():
    Code:
    printf("a = %p \n", (void *) &a);
    Which brings up another problem:

    Turn ON your warnings and turn UP your warnings to the highest level, and please don't ignore the warnings displayed!

    The code you posted will not compile as presented, or at least with warnings!!

    I think you should study a good book an the C Programming Language. The book would explain this topic and many others, you need to know to program successfully in C.

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by rstanley View Post
    @gajya

    In C, a local variable of the same name as a Global variable, will hide the Global.
    I can see that with my example

    Code:
    #include<stdio.h>  
        int a = 1;
        
        int b = 2;
      
    int main()
    {
        int a = 10;
        
        int b = 20;
        
        printf("a = %d \n", a);
        printf("b = %d \n", b);
     
     
           printf("a = %p \n", &a);
        printf("b = %p \n", &b);
         
        return 0;
    }
    a = 10
    b = 20
    a = 0061FF2C
    b = 0061FF28

    Quote Originally Posted by rstanley View Post
    Simple solutions:

    1) Don't use global variables if at all possible! Define them in main() (Or some other called function), then pass them as a value or as a pointer to other functions.

    2) Don't define a Global variable and a Local variable with the same name!!!
    I appreciated the advice but can you explain reason why not with same name

    Quote Originally Posted by rstanley View Post
    I think you should study a good book an the C Programming Language. The book would explain this topic and many others, you need to know to program successfully in C.
    When I was reading the book this thought came in mind

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    They are two different variables, the local in the stack segment, and the global in the global data segment! The local will HIDE the global of the same name, and you, or someone else maintaining your program later, might get confused.

    Globals are usually bad programming practice as any function in the program could alter the value, and make it hard to trace where the global was changed, if an error occurs!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hence gcc has a specific warning to detect these kinds of feature abuse.
    Code:
    $ gcc -Wshadow foo.c
    foo.c: In function ‘main’:
    foo.c:8:9: warning: declaration of ‘a’ shadows a global declaration [-Wshadow]
         int a = 10;
             ^
    foo.c:2:9: note: shadowed declaration is here
         int a = 1;
             ^
    foo.c:10:9: warning: declaration of ‘b’ shadows a global declaration [-Wshadow]
         int b = 20;
             ^
    foo.c:4:9: note: shadowed declaration is here
         int b = 2;
             ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  2. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  3. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  4. Replies: 3
    Last Post: 04-09-2007, 01:11 AM
  5. Assigning an address to a global variable.
    By edunia11 in forum C Programming
    Replies: 9
    Last Post: 08-11-2006, 12:52 PM

Tags for this Thread