purpose of passing the address in a memory

hi guys.


if you have have variable a and b AND pass the address of variable a to b, then b will going to have the address of a. what happens when you are passing an address to other variable?? is there any significance??

help me guys.. really a newbie.. :<
Last edited on
Nothing special, really. You just have b containing a number representing the memory address of a.
1
2
3
4
5
6
7
8
9
10
11
12
int b = 999;
int *a = &b; //a save the address of b(a point to b)
int * c = &b;//c save the address of b(c point to b)
cout<<b<<endl;
*a = 777;
cout<<b<<endl;
*c = 888;
cout<<c<<endl;
int d = 333;
a = &d;(a point to d)
cout<<d<<endl;
cout<<*a<<endl;

read some books(I would recommend c++ primer or c++ for programmers)
and write the code by yourself and you will know how and why
the most important thing of pointer is
"you have to know where are your pointer point to"(just my opinion)
Last edited on
@stereo:

Lines 5 and 7 are really dangerous. I don't think you intended to, but you are simply changing the pointers to point to random memory, not actually changing what is being pointed to.
Sorry, I forgot to dereference it@_@
Topic archived. No new replies allowed.