Jump to content

  • Log In with Google      Sign In   
  • Create Account


Confused by Pointers (Beginner's Script):


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 04 December 2012 - 07:06 PM

// more pointers
#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;
}

I understand all but this line of the script, which I don't see any reason for:
p1 = p2;		   // p1 = p2 (value of pointer is copied)

I can see that it says it was copied, but I thought that already happened in the line above that?
*p2 = *p1;		 // value pointed by p2 = value pointed by p1

Edited by Youbar, 04 December 2012 - 07:12 PM.


Sponsor:

#2 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 04 December 2012 - 07:49 PM

How does this not have any views at all?

#3 Brother Bob   Moderators   -  Reputation: 7903

Like
1Likes
Like

Posted 04 December 2012 - 08:03 PM

What is it you find confusing about the two lines? One assigns pointers, the other assigns the values pointed to by the pointers. The line p1=p2 simply means that the two pointers point to the same address, while *p2=*p1 assigns the values pointed to by the two pointers.

#4 Youbar   Members   -  Reputation: 122

Like
0Likes
Like

Posted 04 December 2012 - 08:06 PM

Oh, I see now. Thanks for clearing that up for me.

#5 Khatharr   Crossbones+   -  Reputation: 2935

Like
0Likes
Like

Posted 04 December 2012 - 08:33 PM

If there's no views it just means that nobody has looked yet.

Traffic varies.

Breathe deeply. Posted Image
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#6 molehill mountaineer   Members   -  Reputation: 595

Like
0Likes
Like

Posted 05 December 2012 - 04:43 AM

A pointer contains the address of something, in the line above the asterisk "dereferences" the pointer (meaning it goes to the location the pointer is pointing to).
The first line says "pointer1 now points to the same thing as pointer2",
the second line says "the thing that pointer1 is pointing to is now the same as the thing that pointer2 is pointing to".
* and & can be a bit confusing for beginners because sometimes it depends on the context. For me it helps to put it into a phrase in this manner.

#7 rip-off   Moderators   -  Reputation: 8066

Like
1Likes
Like

Posted 05 December 2012 - 05:06 AM

The purpose might become clearer when additional output is used:
// more pointers

#include <string>
#include <iostream>


using namespace std;

void print(string description, int value1, int value2, int *pointer1, int *pointer2) {
	cout << description << ": " << endl;
	cout << "  first value is " << value1 << endl;
	cout << "  second value is " << value2 << endl;
	cout << "  first pointer is at " << pointer1 << " with value " << *pointer1 << endl;
	cout << "  second pointer is at " << pointer2 << " with value " << *pointer2 << endl;
	cout << endl;
}


int main()
{
	int firstvalue = 5;
	int secondvalue = 15;
  
	int * p1 = &firstvalue;
	int * p2 = &secondvalue;

	print("Initial values", firstvalue, secondvalue, p1, p2);


	// value pointed by p1 = 10
	*p1 = 10;
	print("After setting *p1 to 10", firstvalue, secondvalue, p1, p2);
  
	// value pointed by p2 = value pointed by p1
	*p2 = *p1;
	print("After settings *p2 to *p1", firstvalue, secondvalue, p1, p2);
  
	// p1 = p2 (value of pointer is copied)
	p1 = p2;
	print("After pointing p1 to p2", firstvalue, secondvalue, p1, p2);
  
	// value pointed by p1 = 20
	*p1 = 20;
	print("After setting *p1 to 20", firstvalue, secondvalue, p1, p2);

	return 0;
}

On my system, this produces:
Initial values:
  first value is 5
  second value is 15
  first pointer is at 0x7fffe8171510 with value 5
  second pointer is at 0x7fffe8171514 with value 15


After setting *p1 to 10:
  first value is 10
  second value is 15
  first pointer is at 0x7fffe8171510 with value 10
  second pointer is at 0x7fffe8171514 with value 15

After settings *p2 to *p1:
  first value is 10
  second value is 10
  first pointer is at 0x7fffe8171510 with value 10
  second pointer is at 0x7fffe8171514 with value 10


After pointing p1 to p2:
  first value is 10
  second value is 10
  first pointer is at 0x7fffe8171514 with value 10
  second pointer is at 0x7fffe8171514 with value 10

After setting *p1 to 20:
  first value is 10
  second value is 20
  first pointer is at 0x7fffe8171514 with value 20
  second pointer is at 0x7fffe8171514 with value 20

As you can see, after that line the two pointers point at the same location, and dereferencing them yields the same value.

Edited by rip-off, 05 December 2012 - 05:07 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS