Address in int

Discussion in 'C Programming' started by john, May 13, 2006.

  1. john

    john Guest

    Is it possible to convert a pointer value to an int?
    like this:
    int *ptr = &blah;
    int address = (int)ptr;

    And is is legal to do:

    printf("%d %d", ptr, &blah);

    ?
     
    john, May 13, 2006
    #1
    1. Advertising

  2. john said:

    > Is it possible to convert a pointer value to an int?
    > like this:
    > int *ptr = &blah;
    > int address = (int)ptr;


    Yes, that's legal, although there is no guarantee that you will not lose
    information during the conversion.

    > And is is legal to do:
    >
    > printf("%d %d", ptr, &blah);


    No, but you can do:

    printf("%p %p\n", (void *)ptr, (void *)&blah);


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 13, 2006
    #2
    1. Advertising

  3. john wrote:
    > Is it possible to convert a pointer value to an int?
    > like this:
    > int *ptr = &blah;
    > int address = (int)ptr;


    Sure.
    On some systems this conversion will truncate the value, though.
    And the way back is not at all guaranteed to work.

    int* ptr = (int*)address;

    > And is is legal to do:
    >
    > printf("%d %d", ptr, &blah);


    No. You need an explicit conversion.
    Image a system where int are two bytes and pointers four.

    printf("%d %d", ptr, (int)&blah);

    In this special case %p might be better, anyway.

    --
    Ich kenne die Mißverständnisse-FAQ, und sie wird oft mißverstanden.
    -- Andreas M. Kirchwitz <>
     
    Alexander Bartolich, May 13, 2006
    #3
  4. john

    Tomás Guest

    john posted:

    > Is it possible to convert a pointer value to an int?
    > like this:
    > int *ptr = &blah;
    > int address = (int)ptr;
    >
    > And is is legal to do:
    >
    > printf("%d %d", ptr, &blah);
    >
    > ?



    It's not portable -- but it will work on most systems.

    It will work on even more systems if you use an "unsigned long long":

    typedef unsigned long long uLL;

    int k;

    uLL address = (uLL)&k;

    (Off-Topic: The C++ Standard says that this will work perfectly, but only
    if the integral type has enough bits to store all off the address's info).

    -Tomás
     
    Tomás, May 13, 2006
    #4
  5. Alexander Bartolich <> writes:
    > john wrote:

    [...]
    >> And is is legal to do:
    >>
    >> printf("%d %d", ptr, &blah);

    >
    > No. You need an explicit conversion.
    > Image a system where int are two bytes and pointers four.
    >
    > printf("%d %d", ptr, (int)&blah);
    >
    > In this special case %p might be better, anyway.


    You need to cast both arguments:

    printf("%d %d", (int)ptr, (int)&blah);

    Though, as others have already pointed out, it makes little sense to
    do this. Use "%p" to print pointers:

    printf("%p %p", (void*)ptr, (void*)&blah);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 13, 2006
    #5
  6. "Tomás" <> writes:
    > john posted:
    >
    >> Is it possible to convert a pointer value to an int?
    >> like this:
    >> int *ptr = &blah;
    >> int address = (int)ptr;
    >>
    >> And is is legal to do:
    >>
    >> printf("%d %d", ptr, &blah);
    >>
    >> ?

    >
    > It's not portable -- but it will work on most systems.


    I would have stopped after "It's not portable".

    > It will work on even more systems if you use an "unsigned long long":
    >
    > typedef unsigned long long uLL;
    >
    > int k;
    >
    > uLL address = (uLL)&k;
    >
    > (Off-Topic: The C++ Standard says that this will work perfectly, but only
    > if the integral type has enough bits to store all off the address's info).


    It says that what will work perfectly?

    I can believe that the C++ standard would guarantee that
    pointer-to-integer-to-pointer conversion yields the original pointer
    value as long as the integer type is big enough (however it defines
    "big enough"). C makes such a guarantee only for intptr_t and
    uintptr_t, which may or may not exist. But there's no reason to go
    through these gyrations rather than simply using "%p".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 13, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,220
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    457
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    957
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    436
    James Kanze
    Apr 17, 2007
Loading...

Share This Page