Thread: value and address of variable in pointer program

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    value and address of variable in pointer program

    I have written simple program to understand basic of pointer in c
    Code:
    #include <stdio.h>
    int main(void)
    {
         /*declaration of  variable*/
     unsigned int number;   
     
     /*declaration of pointer*/
        unsigned int *pointer;  
        
     /*assigning address of variable*/
     pointer=& number; 
        
     /*assigning value 100 to variable*/
     number=100;   
        
       
       printf("value of variable: %d\n address of variable : %u\n",number,&pointer);
        
     
       return 0;
    }
    output

    value of variable: 100
    address of variable : 6422312

    I don't understand meaning of that pointer store value of variable and address of variable. please help with this program

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by vead View Post
    I have written simple program to understand basic of pointer in c
    Code:
    #include <stdio.h>
    int main(void)
    {
         /*declaration of  variable*/
     unsigned int number;   
     
     /*declaration of pointer*/
        unsigned int *pointer;  
        
     /*assigning address of variable*/
     pointer=& number; 
        
     /*assigning value 100 to variable*/
     number=100;   
        
       
       printf("value of variable: %d\n address of variable : %u\n",number,&pointer);
        
     
       return 0;
    }
    output

    value of variable: 100
    address of variable : 6422312

    I don't understand meaning of that pointer store value of variable and address of variable. please help with this program
    Replace the printf line with this:

    Code:
    printf("value of variable: %d\n address of variable : %p\n",number,pointer);
    One output:

    Code:
    value of variable: 100
     address of variable : 0x7ffe81407904

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    In addition to the comment above, for completion, I think what you were originally printing should be explained. You were not printing the address of the original object (number) but the address of the pointer to that object (&pointer is a pointer to pointer; I don't like the term "address of" because it's kind of ambiguous and saying '& gives a "pointer to"' is always correct so why not use that terminology instead? So, using this new terminology, &number is a pointer to an object of type unsigned int and &pointer, in your code, is a pointer to an object of type pointer to unsigned int; i.e. a pointer to a pointer).

    Edit: "Ambiguous" might not be the right word, but I think the C Standard and authors make/made a mistake referring to the & operator as the "address of" operator. It's much better thought of as the "give me a pointer" operator because that is what it actually does. This way you don't have to struggle to understand two pesky terms (address and pointer) because pointer is the only thing and it covers everything properly. &bar is a pointer to bar. If you want to store the pointer then you need a pointer to store the, well, pointer in: unsigned int *foo = &bar; foo is a pointer and &bar is a pointer.
    Last edited by Hodor; 01-04-2018 at 06:33 AM.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by ordak View Post
    Replace the printf line with this:
    I replaced that line
    value of variable: 100
    address of variable : 0061FF28

    but still I have confusen I don't understand what's the benefit of doing this all

  5. #5
    Guest
    Guest
    You said you wrote this program to learn about pointers. What specifically are you confused about?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It might help to actually use pointers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void display(int *pointer) {
        printf("value of variable: %d, address of variable: %p \n", *pointer, (void *)pointer);
    }
    
    int main(void) {
        int number = 100;
        int *pointer;
        
        /* one way to use pointers is to take the address of a normal variable.
        this lets you use a variable using another name, which may be
        necessary. for instance, you may need to call functions with pointers 
        in the signature. */
        pointer = &number;
        display(pointer);   /* same as display(&number); */
        
        /* another important reason to use pointers is dynamic memory (sometimes called
        heap memory) from allocation functions like malloc. using a pointer is the only
        way to refer to this memory by name, use it, or change it. */
        pointer = malloc(sizeof(*pointer)); /* same as sizeof(int) */
        
        /* always check the return value of allocation functions */
        if (pointer == NULL) {
            perror("malloc");
            return 0;
        }
        
        /* at this point, with the memory acquired, we can give the memory a value by 
        dereferencing. do this to change anything. */
        *pointer = number;
        *pointer *= 2;
        ++(*pointer);
        
        /* you will notice that the memory has a completely different address from number */
        display(pointer);
        
        /* free memory from malloc. */
        free(pointer);
        
        return 0;
    }
    value of variable: 100, address of variable: 000000000062FE44
    value of variable: 201, address of variable: 0000000000196A90

    I still recommend the Binky video for one's first introduction to pointers:
    YouTube
    Last edited by whiteflags; 01-04-2018 at 05:08 PM.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by Guest View Post
    You said you wrote this program to learn about pointers. What specifically are you confused about?
    I can pointer store two things, value in variable and address of that variable. I don't what's the benefits of storing address

    int number = 100
    value of variable: 100
    address of variable : 0061FF28

    I can see 100 is store in to variable number and address of variable number is also 0061FF28 stored

    I don't understand what's the benefits of storing address in pointer

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Stop saying "address". You can have two things: a) a variable; and b) a pointer to a variable.

    The benefit of having a pointer is that you can then change the value of a variable when you pass it to a function -- C only has pass by value and no pass by reference so the only way you can change the value of a variable in a function is by passing into the function a pointer to the variable. E.g:

    Code:
    void myfunc(int *p)
    {
         /* p is a pointer to a variable of type int */
    
        *p = 100;     /* Change the value of what's pointed to to 100 (/
    }
    
    int main(void)
    {
         int var = 3;
    
         myfunc(&var);     /* pass myfunc() a pointer to var */
         printf("Value of var is %d\n", var);
    
         return 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    First you may need to read a reliable book on C.

  10. #10
    Guest
    Guest
    Quote Originally Posted by vead View Post
    I can pointer store two things, value in variable and address of that variable. I don't what's the benefits of storing address

    int number = 100
    value of variable: 100
    address of variable : 0061FF28

    I can see 100 is store in to variable number and address of variable number is also 0061FF28 stored

    I don't understand what's the benefits of storing address in pointer
    I hope the replies that followed cleared things up for you.

    A pointer only stores the address (yes, I happen to like "address") of a variable, nothing else -- so there is no duplication of data.
    Code:
    int i = 5;     // address: FF00, data: 5
    int *p = &i;   // address: FF08, data: FF00
    p does not contain 5, but you can get the value of i through dereferencing it. p is a bit like a middleman.

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Guest View Post
    A pointer only stores the address (yes, I happen to like "address") of a variable
    The problem is that &foo is not an address... it's a pointer Going further, what does "address" mean anyway?

  12. #12
    Guest
    Guest
    Well, I always had this mental model and didn't perceive it to be confusing:
    Code:
    int foo;    // foo has/is/refers-to an address
    int *ptr;   // ptr ... an address
    int **ptr2; // ptr2 ... an address
    
    foo = 5;      // foo stores value (yes, it's all bits and bytes either way, I know)
    ptr = &foo;   // ptr stores address of foo
    ptr2 = &ptr;  // ptr2 stores address of ptr
    I.e. objects have addresses, and pointers are objects storing addresses. I see though, how your model works all the same and with one component less.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Code:
    #include<stdio.h>
    int main (void)
    {
     int i, number[4];
     int *pointer;
     number [4] = {1,2,3,4};
        pointer = &number[4];
     
     for (i = 0, i <4; i++)
      
      {
       
            printf("The value of var is: %d \n", number[4]);
     
            printf("The address of var : %p \n", &pointer);         
      } 
     
        return 0;
    }
    program error

    hello.c: In function 'main':
    hello.c:6:15: error: expected expression before '{' token
    number [4] = {1,2,3,4};
    ^
    hello.c:9:23: error: expected ';' before ')' token
    for (i = 0, i <4; i++)
    ^
    What's wrong

  14. #14
    Guest
    Guest
    You really should start with a book. This is not an effective way to learn.

    1. number[4] does not exist, in programming we start counting at 0.
    2. You cannot assign four values to an element. You'd have to use the brace-syntax while declaring the variable.
    3. A for loop separates the three argument groups by semi-colon, not comma.
    4. Get a book already, will ya?

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Guest View Post
    Well, I always had this mental model and didn't perceive it to be confusing:
    Code:
    int foo;    // foo has/is/refers-to an address
    int *ptr;   // ptr ... an address
    int **ptr2; // ptr2 ... an address
    
    foo = 5;      // foo stores value (yes, it's all bits and bytes either way, I know)
    ptr = &foo;   // ptr stores address of foo
    ptr2 = &ptr;  // ptr2 stores address of ptr
    I.e. objects have addresses, and pointers are objects storing addresses. I see though, how your model works all the same and with one component less.
    My mental model is pretty much the same as your (if not, exactly the same), but I learned ASM before C. I don't know though... having two terms for what, in C, is the same abstract concept might be what confuses newcomers when it comes to pointers (I don't know for sure because I never had a problem grokking pointers in the first place)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2015, 07:43 AM
  2. Get address of variable from pointer
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2007, 10:50 AM
  3. obtaining a variable's address
    By Bleech in forum C Programming
    Replies: 5
    Last Post: 09-06-2006, 11:39 PM
  4. Storing an IP Address in a Variable?
    By guitarlover317 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2006, 07:54 AM
  5. may address of a variable be null
    By hebele in forum C Programming
    Replies: 5
    Last Post: 07-01-2005, 02:16 PM

Tags for this Thread