New to pointers

This is a discussion on New to pointers within the C++ Programming forums, part of the General Programming Boards category; Hi, I sort of understood pointers, until I went to cplusplus.com and saw this code: Code: #include <iostream> using namespace ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22

    New to pointers

    Hi, I sort of understood pointers, until I went to cplusplus.com and saw this code:

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int firstvalue = 5, secondvalue = 15;
      int * p1, * p2;
    
      p1 = &firstvalue;  // p1 = address of firstvalue
      p2 = &secondvalue; // p2 = address of secondvalue
      *p1 = 10;          // value pointed by p1 = 10
      *p2 = *p1;         // value pointed by p2 = value pointed by p1
      p1 = p2;           // p1 = p2 (value of pointer is copied)
      *p1 = 20;          // value pointed by p1 = 20
      
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      return 0;
    }
    the output is:

    firstvalue is 10
    secondvalue is 20

    Ok, what I dont understand is why first value is 10 and not 20. I would really appreciate any simple explanation.

    Oh and...Is there something I could think about or remember so that I dont get confused with pointers?

    THANKS

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    The value of firstvalue is changed via p1. p1 is not a copy; it actually points to the value.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
      int firstvalue = 5, secondvalue = 15;
      int * p1, * p2;
    
      cout << "begiinging" << endl;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
      p1 = &firstvalue;  // p1 = address of firstvalue
      p2 = &secondvalue; // p2 = address of secondvalue
    
      cout << "after pointer assignments (nothing)" << endl;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
      *p1 = 10;          // value pointed by p1 = 10
    
      cout << "firstvalue changes" << endl;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
    
      *p2 = *p1;         // value pointed by p2 = value pointed by p1
    
      cout << "secondvalue changes" << endl;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
    
      p1 = p2;           // p1 = p2 (value of pointer is copied)
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
      *p1 = 20;          // value pointed by p1 = 20
      
      cout << "secondvalue changes" << endl;
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
    
      return 0;
    }
    Quote Originally Posted by Tesnik View Post
    Oh and...Is there something I could think about or remember so that I dont get confused with pointers?
    bunnies
    THANKS
    you're welcome

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    I dont see why *p1 = 20 makes second value change. p1 is the reference to the firstvalue, so why does secondvalue change and not the firstvalue.

    ive tried with the bunnies... they dont work.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    For now you have this:

    Code:
    p1 ----> firstvalue
    p2 -----> secondvalue
    Code:
    p1 = p2;           // p1 = p2 (value of pointer is copied)
    Now you have this:

    Code:
    firstvalue
    
    p1 --------------> secondvalue <------------- p2
    The first one makes p1 equal to p2. This means they both point to secondvalue.

    Code:
    *p1 = 20;          // value pointed by p1 = 20
    Now you take whatever p1 points to, which is secondvalue, and change it to 20.

    Make sense now?

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    yes!

    thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 03:04 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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