Tutorial question regarding pointers

In the tutorial it has this example talking about pointers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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;
}


Shouldn't line 14 say: // p1 = address of p2 ?
talking about values as "the stuff in the memory location" then to refer to value as "a memory address" makes no sense. Or do I have this wrong?
Last edited on
p2 is a pointer, this means that the value of p2 is a memory address. ( This is the definition of the term "pointer" )
p1 = p2 is an assignment, it copies the value of p2 to p1 ( this is what assignment does with any type )
so the value of p2 ( which is a memory address but it isn't the memory address of p2 ) is copied to p1

What you are saying is like this:
1
2
3
int **p1; // pointer to pointer
int *p2; // just pointer
pi = &p2 // p1 = address of p2 
Bazzy++;
Notice that there are expressions with pointers p1 and p2, both with and without dereference operator (*). The meaning of an expression using the dereference operator (*) is very different from one that does not: When this operator precedes the pointer name, the expression refers to the value being pointed, while when a pointer name appears without this operator, it refers to the value of the pointer itself (i.e. the address of what the pointer is pointing to).


This was taken from the following paragraph, it makes me think that while:
lets say the memory address of p1 is 12345, and the value of that memory address is a string "Hello world";

*p2 = *p1; // = the value pointed by p1 ("Hello world") = the value pointed by p2, So now p2 points to the same block of memory 12345. ?

and the normal assignment:

p1 = p2; // = p1 is now the address of p2.
while when a pointer name appears without this operator, it refers to the value of the pointer itself(i.e. the address of what the pointer is pointing to)



why does it say "the address" of what the pointer is pointing to
Last edited on
like if the "value" of p1 (without the preceeding operator) is not the memory address or the 'thing' stored in the memory what is it?
p1 = p2; // = p1 is now the address of what p2 is pointing to
while when a pointer name appears without this operator, it refers to the value of the pointer itself(i.e. the address of what the pointer is pointing to)

address of p2 != address of what p2 is pointing to
value of p2 == address of what p2 is pointing to

*p2 = *p1; // = the value pointed by p1 ("Hello world") = the value pointed by p2, So now p2 points to the same block of memory 12345. ?
No, see this example:
1
2
3
4
5
int x = 5, 
    y = 6;
int *p1 = &x,
    *p2 = &y;
*p2 = *p1;
x = 5,   &x = 12345
y = 6,   &y = 67890
p1 = 12345, *p1 = 5
p2 = 67890, *p2 = 6
p2 = 67890, *p2 = 5, y = 5    ( same as y = x )                                    
Last edited on
The result of the first statement depends on the type of string and what type of memory p2 points to. If it's a char * and p2 points to writable memory, then (if we assume p2 pointed to a string "something") p2 points to a string "Homething". If p2 points to a char, but the memory is read-only, then the statement will cause the program to crash. If both pointers point to std::strings, then the stament will copy the entire string from *p1 to *p2.

The second statement always does the same no matter what: it copies one pointer to another. Be careful with your language, however. It doesn't copy the address of p2 to p1. It copies the address of *p2 to p1, or in other words, it copies p2 to p1.
address of p2 != address of what p2 is pointing to


OHHHhh, so the pointers have there own memory address seperate from the memory they point to?
I assume yes after what helios just wrote. Thanks guys, that makes 10000x more sense now :D

I was thinking the address of the pointer was the same as what it pointed to. I don't know how that logic actually worked in real life computing terms, I just assumed it was the way it was. hahaha
Last edited on
haha. obviously you didn't finish the tutorials on pointers yet.. i'd say keep on reading.
I was thinking the address of the pointer was the same as what it pointed to.
There's only one situation where that would be the case:
void *p=(void *)&p;
I have read it a couple times month or two ago and didn't fully grasp it (it made sense), but never tried any example code or used them since. So that time I was computing all examples in my head before reading the comments and paragraphs, and then was stumped by the below paragraph saying they weren't the same. Of course I was going by my retard logic of pointers being the memory address of whatever they point to. :s
Last edited on
I have to try: void *p=(void *)&p;

the value which p points to in memory = (the value pointed to ) the memory address of p.
Last edited on
@helios: Can you teach me how to divide by zero?
Topic archived. No new replies allowed.