Pointers

int *p is a pointer of an int type ,
int m=20 ;

whats the difference between
p=m,*p=m and p=&m ? are they same ?
Last edited on
no, in the first instance

p = m;

the first won't compile.

in the second you are assigning the value of integer m to the object the pointer 'p' points to.

in the third you are assigning the address of the memory location 'm' to the pointer 'p'.
In addition to what pogrady said, even though your last two statements results in the same value for p, it is technically different.

Your third statement passes the address so any changes made to int m will cause p to change. Your second statement only sends a copy of the value of m to pointer p so if m is changed later, the value of p will not change.

Ex:

1
2
3
4
5
6
7
8
int m = 100;
int *ptr = &m;

cout << *ptr;

m = 400;

cout << *ptr;


100 then 400 is displayed since the address of m is passed to pointer p.
Topic archived. No new replies allowed.