changing the address of a pointer question..

This is a discussion on changing the address of a pointer question.. within the C Programming forums, part of the General Programming Boards category; so (foo_ptr = 42 ; ) will change the address of the variable foo but the value of foo will ...

  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
    20,980
    The return value of &foo_ptr is an rvalue; it cannot legally appear on the left hand side of an assignment.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    but if we want to change the address of "foo" variable
    No. As Elysia has noted, "you cannot change the address of a variable".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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
    20,980
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,185
    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,047
    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,185
    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,047
    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

Page 2 of 3 FirstFirst 123 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21