Storing address of pointer (cross platform)

Discussion in 'C++' started by howa, Jan 19, 2008.

  1. howa

    howa Guest

    In 32 bit pc, I can write

    e.g.

    char* p1 = "apple";
    cout<<(int)p1;


    In 64 bit pc, I need to use

    char* p1 = "apple";
    cout<<(__int64)p1;


    What are the cross platform way to so?

    Thanks.
     
    howa, Jan 19, 2008
    #1
    1. Advertising

  2. On 2008-01-19 13:47, howa wrote:
    > In 32 bit pc, I can write
    >
    > e.g.
    >
    > char* p1 = "apple";
    > cout<<(int)p1;
    >
    >
    > In 64 bit pc, I need to use
    >
    > char* p1 = "apple";
    > cout<<(__int64)p1;
    >
    >
    > What are the cross platform way to so?


    If your platforms have C compilers providing <stdint.h> you can check if
    they define the typedef intptr_r or uintptr_t, they should be large
    enough for a void pointer. If you do not have those typedefs you need to
    provide them (or similar) on your own.

    --
    Erik Wikström
     
    Erik Wikström, Jan 19, 2008
    #2
    1. Advertising

  3. howa

    howa Guest

    On 1月19æ—¥, 下åˆ9時12分, Erik Wikström <> wrote:
    > On 2008-01-19 13:47, howa wrote:
    >
    > > In 32 bit pc, I can write

    >
    > > e.g.

    >
    > > char* p1 = "apple";
    > > cout<<(int)p1;

    >
    > > In 64 bit pc, I need to use

    >
    > > char* p1 = "apple";
    > > cout<<(__int64)p1;

    >
    > > What are the cross platform way to so?

    >
    > If your platforms have C compilers providing <stdint.h> you can check if
    > they define the typedef intptr_r or uintptr_t, they should be large
    > enough for a void pointer. If you do not have those typedefs you need to
    > provide them (or similar) on your own.
    >
    > --
    > Erik Wikström


    Thanks.
     
    howa, Jan 19, 2008
    #3
  4. howa:

    > In 32 bit pc, I can write
    >
    > e.g.
    >
    > char* p1 = "apple";
    > cout<<(int)p1;
    >
    >
    > In 64 bit pc, I need to use
    >
    > char* p1 = "apple";
    > cout<<(__int64)p1;
    >
    >
    > What are the cross platform way to so?



    I'm not sure if cout can print a pointer, but printf certainly can:

    int i;

    printf("%p",(void*)&i);

    --
    Tomás Ó hÉilidhe
     
    Tomás Ó hÉilidhe, Jan 19, 2008
    #4
  5. howa

    Rolf Magnus Guest

    howa wrote:

    > In 32 bit pc, I can write
    >
    > e.g.
    >
    > char* p1 = "apple";
    > cout<<(int)p1;
    >
    >
    > In 64 bit pc, I need to use
    >
    > char* p1 = "apple";
    > cout<<(__int64)p1;
    >
    >
    > What are the cross platform way to so?


    cout << static_cast<void*>(p1);
     
    Rolf Magnus, Jan 19, 2008
    #5
  6. howa

    James Kanze Guest

    On Jan 19, 1:47 pm, howa <> wrote:
    > In 32 bit pc, I can write


    > e.g.


    > char* p1 = "apple";
    > cout<<(int)p1;


    > In 64 bit pc, I need to use


    > char* p1 = "apple";
    > cout<<(__int64)p1;


    (A more portable 64 bit type would be long long.)

    > What are the cross platform way to so?


    cout << static_cast< void* >( p1 ) ;

    It's the only way guaranteed to work everywhere.

    It doesn't allow you to control the output format in any way,
    however. If you need to control the output format, something
    like:
    cout << static_cast< uintptr_t >( p1 ) ;
    will usually work (but uintptr_t is not guaranteed to be present
    on a machine where pointers are larger than 64 bits).

    If you have to work with an older compiler, which doesn't
    support the new integer types, then casting to size_t is often
    OK (although I've used platforms where it wouldn't work).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 20, 2008
    #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. jimjim
    Replies:
    16
    Views:
    861
    Jordan Abel
    Mar 28, 2006
  2. toton
    Replies:
    11
    Views:
    731
    toton
    Oct 13, 2006
  3. joamag
    Replies:
    3
    Views:
    335
    Hans Mulder
    Apr 30, 2010
  4. Stanley Rice

    Address of array && address of pointer to array

    Stanley Rice, Sep 14, 2011, in forum: C Programming
    Replies:
    33
    Views:
    1,194
    Keith Thompson
    Sep 20, 2011
  5. junyangzou
    Replies:
    8
    Views:
    192
    junyangzou
    Aug 30, 2013
Loading...

Share This Page