Pointer declare

Pages: 12
#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 to by p1 = 10
*p2 = *p1; // value pointed to by p2 = value pointed to by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed to by p1 = 20

cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}

why secondvalue is 20?
i no understand this programming pls explain to me tq
secondvalue = 15; begins with value of 15

p2 = &secondvalue; p2 points at secondvalue

p1 = p2; p1 now also points at secondvalue

*p1 = 20; secondValue set to 20
$ gdb ./a.out
(gdb) break main
Breakpoint 1 at 0x11b2: file foo.cpp, line 4.
(gdb) run
Breakpoint 1, main () at foo.cpp:4
4	int main() {
(gdb) next
5		int firstvalue = 5, secondvalue = 15;
(gdb) next
8		p1 = &firstvalue;  // p1 = address of firstvalue
(gdb) watch secondvalue 
Hardware watchpoint 2: secondvalue
(gdb) continue 
Hardware watchpoint 2: secondvalue
Old value = 15
New value = 10
main () at foo.cpp:12
11		*p2 = *p1;         // value pointed to by p2 = value pointed to by p1
12		p1 = p2;           // p1 = p2 (value of pointer is copied)
(gdb) continue
Hardware watchpoint 2: secondvalue
Old value = 10
New value = 20
main () at foo.cpp:15
13		*p1 = 20;          // value pointed to by p1 = 20
14
15		cout << "firstvalue is " << firstvalue << '\n';
(gdb) quit
the watchpoint stops at the line after the assignment, put the previous line for context
Last edited on
@limchankhim

Isn't it all explained here?

http://www.cplusplus.com/doc/tutorial/pointers/
to : Repeater (1791)


secondvalue = 15; begins with value of 15

p2 = &secondvalue; p2 points at secondvalue number?= number?

p1 = p2; p1 now also points at secondvalue number?= number?

*p1 = 20; secondValue set to 20 number?= number?

have a video sample ?
or u have wechat facebook let me know
Last edited on
You have:
1
2
3
4
int firstvalue = 5;
int * p1;
p1 = &firstvalue; // p1 = address of firstvalue
*p1 = 10; // value pointed to by p1 = 10 

Why doesn't the "firstValue set to 10" confuse you too?
1
2
int x = 3;
x = 4;


After this example code, x is 4. Do you understand that?
keskiverto (8061) & Repeater (1792) ok this is understand

i only confuse

*p1 = 20; // value pointed to by p1 = 20

beacuse firstvalue is p1 (Why not *p1 = 20?)

why secondvalue is p2 = 20? ( *p2 is 20?)
Last edited on
p1 and p2 are pointers.

*p1 means "the thing that p1 is pointing at"

*p2 means "the thing that p2 is pointing at"

p1 and p2 point at the same thing
Last edited on
1
2
3
4
5
6
7
8
9
int firstvalue = 5;
int * p1;
p1 = &firstvalue;
*p1 = 10;
// you claim that you understand up to this point

int secondvalue = 15;
p1 = &secondvalue;
*p1 = 20; // what happens here? 
ok tq all teach me i will Reference^^
teach you... do you understand this?
1
2
3
4
5
6
7
8
9
10
11
int x[] = {1,2,3,4,5};
int index = 1;
cout << x[index];
x[index] = 20;
cout << x[index];
index = 2;
cout << x[index];
x[index] = 30;
for(int i = 0; i<5; x++)
 cout << x[i];



index above is exactly like a pointer and the samples you are trying to understand. Index represents the pointer, and x represents the memory of your computer, but its the same idea, if you change index's value, you are looking at a different "array" location (memory location) and if you change what x at the index location (what index points to) it updates the value in the "array" (memory location).

so re looking at the question:
int secondvalue = 15; //secondvalue is a variable, so it is tied to a location somewhere in memory, and at that location in memory are some bytes that represent the value "15".

p1 = &secondvalue; //p1 is an index into memory for the location where secondvalue is stored. Or, its a pointer to the address of the variable. Same idea, different (and more correct and professionally understood) words.
*p1 = 20; // what happens here? //same as x[index] = value above, just different syntax, now its in pointer lingo instead of array terminology. you can actually USE array terminology here, p1[0] = 20; is an identical statement.

Important: they are not really the same things, of course. But this will help you understand the IDEA, I hope.


Last edited on
jonnin (3601) :

no no no this is make me more confuse i want learn basic first
Repeater (1797) :

Hi i have understand

p1 = &firstvalue;
p2 = &secondvalue;
*p1 = 10;
*p2 = *p1;
p1 = p2; //p1(this is firstvalue ) = p2 (this is secondvalue )
*p1 = 20; // *p1(this is firstvalue =10) = 20(this is secondvalue=20 ) right?
Last edited on
*p1 = 20; // *p1(this is firstvalue =10) = 20(this is secondvalue=20 ) right?


Why is this line talking about firstvalue? Nothing on this line has anything to do with firstvalue. firstvalue is completely irrelevant. p1 does not point at firstvalue, p2 does not point at firstvalue.
Repeater (1801) :

Do you have any other teachings that make me more understand TT?
Last edited on
thanks
Repeater (1802)

can explain to me firstvalue? thanks
What "firstvalue"? You did already claim that you do understand:
1
2
3
4
int foo = 5;
int* bar;
bar = &foo;
*bar = 10;

Well, do you?
Pages: 12