change pointer in c++

hi guys I am c++ user and i have question about changing pointer
exsample

1
2
3
4
5
int x = 1;
int y = 2;
int*z = &x;
int*w = z;


now i want to change z pointer to from x to y using w assuming i dont have z.
so if i said
w = &y; that mean i cahnged w itself or
*w = y; i changed w,z's value
can you help me guys? thanke you :)
Last edited on
DragonForce99 wrote:
w = &x; that mean i cahnged w itself

Yes, you changed w. It now points to x.

DragonForce99 wrote:
*w = x; i changed w,z's value

This changes the value that w points to. If w is pointing to y that means both x and y will now have the same value.
i know that so that why i asked you is there any way to change z's pointer using w assuming w is pointing to z and z is not defined in my function
I think I know what you are asking. If 'w' was a pointer to a pointer int ** w; then yes you can do this. As you have it written now though it won't work.
In your code w is not pointing to z. w is pointing to x. If you want w to be able to point to z you will have to change the type to int** (pointer to pointer to int).

1
2
3
4
5
6
int x = 1;
int y = 2;
int* z = &x;
int** w = &z;

*w = &y; // z will now point to y 

Last edited on
i am not sure about what you are talking about but i'll tell you this
z is pointing to x w is pointing to z but its not definded in my function

using w i want z point to y

tell me how to do it plese
aha it's working thank you and much appreciate it :) thank you
Topic archived. No new replies allowed.