Thread: Casting number to pointer

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    23

    Casting number to pointer

    Hello,

    I am casting an integer value to a pointer. It works without problems on systems where sizeof(int) == sizeof(pointer)

    But on systems where this is not the case, I am getting a compiler warning.
    How can I do the number to pointer cast, so that it works everywhere without warnings?

    The context is:
    User can input a number which is interpreted as an address. He can look up the value at that address.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe explain why you need to do such a thing.

    "It happens to work" isn't a good design decision.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    The user inputs a number in the console, which is to be interpreted as an address.
    I then have to lookup what is at that address and tell him.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I wonder how could that work.
    Since your program will probably crash when you try to 'lookup what is at that address'.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So why not make the variable a pointer to begin with? Why lie to the compiler and tell it it's an int?

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    Is it possible to read numbers from the console as addresses then?

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    scanf got %p. look up the manual.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The user inputs a number in the console, which is to be interpreted as an address.
    scanf accepts %p as a format - why not use that?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > The user inputs a number in the console, which is to be interpreted as an address.
    scanf accepts %p as a format - why not use that?
    As others have already pointed out this needs exceptional care...

    Code:
    Please enter the address to look up:  -1
    OOPS....

    The real question here is: What possible use could this serve?

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    Because I didn't know.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Pointers (addresses) point to a place in memory. There is no predictable way a user would know to enter in a valid address when the program is executing since memory allocation is done dynamically... and possibly changes with each execution of the program even if you found out an example. Therefore it is not reasonable to have a user input such a value and expect it to point to anything significant. UNLESS you are attempting to read any random area of memory that's not necessarily within the scope of the executing program. The operating system would have set up fence areas and give stern warnings if you tried to access forbidden areas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM