Thread: changing the address of a pointer question..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
    ??

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The return value of &foo_ptr is an rvalue; it cannot legally appear on the left hand side of an assignment.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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
    Code:
    foo_ptr = 42;  //for example
    thanks

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but if we want to change the address of "foo" variable
    No. As Elysia has noted, "you cannot change the address of a variable".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    No. As Elysia has noted, "you cannot change the address of a variable".
    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.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
    ??

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    what does this value 42 change in this line
    It changes the value of foo_ptr, though more accurately you would write:
    Code:
    foo_ptr = (int*)42;
    But now, attempting to dereference foo_ptr probably leads to undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so the pointer foo_ptr will point on address 42 ??

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is to say, the odds of address 42 actually existing and belonging to your program are small.

  12. #27
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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:

    Code:
    foo_ptr = (int*)42;
    unlike

    Code:
    foo_ptr = (int)42;  //without *

    ??

  13. #28
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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 *.

  15. #30
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM
  3. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM