Understand pointers

This is a discussion on Understand pointers within the C Programming forums, part of the General Programming Boards category; Hi, I'm currently going through the beginner tutorial in C. I understand the function of pointers, thanks to the comparation ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Understand pointers

    Hi, I'm currently going through the beginner tutorial in C. I understand the function of pointers, thanks to the comparation with the deposit boxes. I know that a pointer stores the memory adress of a variable but I'm having problems understanding how the &-operator works, especially in the printf- and scanf functions:

    Code:
     p = &x;           
     scanf( "%d", &x );  
     printf( "%d\n", *p );
    Could someone explain this?

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Assuming p is an int *, and x is an integer:
    Code:
    p = &x; // Set p to the address of the memory location where x is to be stored
    scanf("%d", &x); // Get an integer from the user and store in the memory location for x
    printf("%d\n", *p); // Go to the memory location pointed to by p and print out the integer value contained there

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    scanf needs to know where to put the data it is scanning from input. If you simply pass a variable instead of a pointer to a location/variable then that value (the variable you are passing in) will only be a copy of the variable because that's how such things are passed into functions. Any changes to such a copied value only affects that copy and when the function ends you'll find that nothing in the original (passed-in) variable has changed. A pointer/address, even if it is a copy, is still a copy of the actual address and so writing to that location via a pointer (copied pointer) will still put the data in the correct location such that when the function ends the parsed information winds up where it's supposed to be. So, in order to actually save data, scanf needs an address.

    The printf function on the other hand does not need to get anything and therefore does not need to know about the address of the passed in variable, just the value. So, using a pointer, you must dereference said pointer by use of the '*' in front of the pointer variable name to get the value pointed to by that pointer, otherwise the printf function would essentially be operating on the assumption that it was the value of the pointer (the address) that you wanted to print and not the value stored at that address.

    Assuming:
    Code:
    int x;
    int * p = &x;  /* Store address of x into pointer p */
    Then any of the following read operations would work to load data into x:
    Code:
    scanf("%d",&x); /* Store into where ever x is located in memory */
    scanf("%d",p);  /* Store into location pointed to by p (p holds address of x) */
    Also, any of following write operations would output the value stored in x
    Code:
    printf("%d\n",x);  /* Output the value stored in x */
    printf("%d\n",*p); /* Output value of what's stored at where p points to (same location as x) */
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User shrikanth's Avatar
    Join Date
    Aug 2010
    Location
    Banglore, India
    Posts
    5

    Smile

    Quote Originally Posted by Aphex View Post
    Hi, I'm currently going through the beginner tutorial in C.
    lets just replace ur variables x and p by something interesting,
    lets think ur home "address" is amity_villa.
    i,e
    amity_villa=address of Aphex's home.
    or

    amity_villa=&Aphex_home;

    scanf("%d",&Aphex_home);

    as u can see u r asking some one to come ur home, lets think ur girlfriend anjelina_joulie by giving ur home address, so by this statement ur gf anjelina has come ur home amity_villa so she is the "value" at amity_villa.

    now, if u want to print the value at address amity_villa (joulie) then u'l hav to use the * operator like this *amity_villa

    printf("%d",*amity_villa);
    so this should print ur gf jolie!!

    NOTE:forget abt %d and all that, i'm just tryin to explain pointer address concept.
    Last edited by shrikanth; 08-12-2010 at 10:13 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21