so (foo_ptr = 42 ; ) will change the address of the variable foo but the value of foo
will stay the same
but i thought that
&foo_ptr represents the address of foo
why arent they doing
??Code:&foo_ptr = 42;
so (foo_ptr = 42 ; ) will change the address of the variable foo but the value of foo
will stay the same
but i thought that
&foo_ptr represents the address of foo
why arent they doing
??Code:&foo_ptr = 42;
The return value of &foo_ptr is an rvalue; it cannot legally appear on the left hand side of an assignment.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so we can use &foo_ptr only to print it or use this address as a static value
but not to change the address
but if we want to change the address of "foo" variable
we do
thanksCode:foo_ptr = 42; //for example
No. As Elysia has noted, "you cannot change the address of a variable".but if we want to change the address of "foo" variable
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It would be a bit like you going out on the street and starting to change the numbers on the house... Once you live in a particular house, you can't just change it's number [I'm sure the local government can, but that is something else] - and a variable is located by the "local government", which is this case is the combination of compiler and operating system (the compiler being your "local government", and the OS being the "central government" for the entire system - the compiler still needs to abide by the OS's rules).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ok so if i cant change any address what so ever then
what does this value 42 change in this line
??Code:foo_ptr = 42;
It changes the value of foo_ptr. In other words, foo_ptr no longer points to foo (since foo is still at 123598736), but points over there somewhere.
Edit: 123598736 is a random number.
It changes the value of foo_ptr, though more accurately you would write:what does this value 42 change in this line
But now, attempting to dereference foo_ptr probably leads to undefined behaviour.Code:foo_ptr = (int*)42;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so the pointer foo_ptr will point on address 42 ??
The pointer foo_ptr will contain the address 42. But why would you want to do that?so the pointer foo_ptr will point on address 42 ?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That is to say, the odds of address 42 actually existing and belonging to your program are small.
i cant understand this form of casting you are doing here:
you want to present the number 42 as an integer and put it into foo_ptr
but why you add* (i suppose it has something to do with pointer)
but i cant see what this line mean:
unlikeCode:foo_ptr = (int*)42;
Code:foo_ptr = (int)42; //without *
??
It's the compiler's job to associate the symbol foo with a memory location so the behaviour is undefined if you try to relocate it to another place or it could become a foobar
Because foo_ptr is not an int, so casting to an int is bad and ..., well, bad. foo_ptr is an int *, so the value you assign to it should also be an int *.
It's the compiler's job to associate the symbol foo with a memory location so the behaviour is undefined if you try to relocate it to another place or it could become a foobar