none
Pointers RRS feed

  • Question

  • int firstvalue = 5, secondvalue = 15;

        int * p1, * p2;

        

    p1 = &firstvalue; p2 = &secondvalue; *p1 = 10; *p2 = *p1; p1 = p2; *p1 = 20;

       

    cout << "firstvalue is " << firstvalue << endl;

        cout << "secondvalue is " << secondvalue << endl;

        return 0;

    }

    Can some one explain to me that how it the final value of firstvalue = 10 and secondvalue = 20.

    I am confused since p1 = 10 than the last time p1 = 20. while p2 is copied the value of p1 which was =10.

    Please explain to me thoroughly. New to this item



    • Edited by Dynamic Kriz Sunday, December 30, 2012 1:24 PM
    • Moved by Jason Dot Wang Tuesday, January 1, 2013 5:57 AM This thread is about Visual C++ (From:Visual C# )
    Sunday, December 30, 2012 1:07 PM

Answers

  •   p1 = &firstvalue;  // Refers to mem address of firstvalue
      p2 = &secondvalue; // refers to mem address of secondvalue
      *p1 = 10;  // Assigns firstvalue 10        
      *p2 = *p1; // Assigns secondvalue 10
      p1 = p2;   // Now p1 refers to mem address of secondvalue.        
      *p1 = 20;  // Assigns secondvalue 20
    
    
     

    http://www.cplusplus.com/doc/tutorial/pointers/
    Sunday, December 30, 2012 1:34 PM
  • Because, as this line

    p1 = p2;   // Now p1 refers to mem address of secondvalue.        

    shows, from that moment on, they're the same, they point to the same adress, reference the same memory; as such, if you assign one, you assign the other.

    Also, this should be in the C++ forum.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    Sunday, December 30, 2012 3:34 PM

All replies

  •   p1 = &firstvalue;  // Refers to mem address of firstvalue
      p2 = &secondvalue; // refers to mem address of secondvalue
      *p1 = 10;  // Assigns firstvalue 10        
      *p2 = *p1; // Assigns secondvalue 10
      p1 = p2;   // Now p1 refers to mem address of secondvalue.        
      *p1 = 20;  // Assigns secondvalue 20
    
    
     

    http://www.cplusplus.com/doc/tutorial/pointers/
    Sunday, December 30, 2012 1:34 PM
  • Once p1 and p2 = 10, then

    p1 = p2 here p1 refers to memory address of p2, than p1 = 20.

    I am confused how come p2 becomes 20.

    Sunday, December 30, 2012 2:47 PM
  • Because, as this line

    p1 = p2;   // Now p1 refers to mem address of secondvalue.        

    shows, from that moment on, they're the same, they point to the same adress, reference the same memory; as such, if you assign one, you assign the other.

    Also, this should be in the C++ forum.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    Sunday, December 30, 2012 3:34 PM
  • Hi Dynamic Kriz,

      Welcome to MSDN Forum Support.

     

      You are more likely to get more efficient responses to Visual  C++ issues at http://social.msdn.microsoft.com/Forums/en/vcgeneral/threads where you can contact Visual C++ experts.

      Sincerely,

      Jason Wang



    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, January 1, 2013 5:56 AM
  • int firstvalue = 5, secondvalue = 15;

        int * p1, * p2;

        

    p1 = &firstvalue; p2 = &secondvalue; *p1 = 10; *p2 = *p1; p1 = p2; *p1 = 20;

       

    cout << "firstvalue is " << firstvalue << endl;

        cout << "secondvalue is " << secondvalue << endl;

        return 0;

    }

    Can some one explain to me that how it the final value of firstvalue = 10 and secondvalue = 20.

    I am confused since p1 = 10 than the last time p1 = 20. while p2 is copied the value of p1 which was =10.

    Please explain to me thoroughly. New to this item



    In addition to Steven, you can print the values after each step for better understanding.

    Thanks, Renjith V R

    Tuesday, January 1, 2013 12:41 PM
  • Hi,

    I think these documentation could help you better understanding the C++ pointers:

    C++ Language Tutorial\Pointers

    Tutorial: Pointers in C++

    Thanks,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 1, 2013 7:03 PM
    Moderator