Re: Address of structure element

Discussion in 'C Programming' started by Fred, Sep 17, 2009.

  1. Fred

    Fred Guest

    On Sep 17, 2:54 pm, Mr John FO Evans <> wrote:
    > I have a structure and a pointer 'point' to that structure.
    >
    > As expected the value of an element is:-
    >    point->element
    >
    > I need the address of that element and find that using (Norcroft ARM C)
    >    address=(int)&point->element    Gives the correct address.
    >    (Possibly (void *) would be better than (int) but in my code
    >    the address is being passed to an emulator which redefines the
    >    type of address. Hence the type here is less important.)
    >
    > On thinking about this I am surprised that:-
    >    address=(int)&(point->element) does not give the correct address
    >    but returns a low value.
    >
    > Can anyone explain this? Should I tackle it differently?
    >

    Be careful! On a 64-bit machine, a pointer is likely to be 64 bits,
    while an
    int is likely to be 32 bits. In that case, your code will break,
    since an int cannot in general hold an address.

    --
    Fred K
     
    Fred, Sep 17, 2009
    #1
    1. Advertising

  2. Fred <> wrote:
    >Be careful! On a 64-bit machine, a pointer is likely to be 64 bits,
    >while an int is likely to be 32 bits. In that case, your code will
    >break, since an int cannot in general hold an address.


    A couple things to try there, if you (the OP) really want it to be an
    int, is to instead use intptr_t, if you have it, or uintmax_t.

    -Beej
     
    Beej Jorgensen, Sep 18, 2009
    #2
    1. Advertising

  3. Fred

    James Kuyper Guest

    Beej Jorgensen wrote:
    > Fred <> wrote:
    >> Be careful! On a 64-bit machine, a pointer is likely to be 64 bits,
    >> while an int is likely to be 32 bits. In that case, your code will
    >> break, since an int cannot in general hold an address.

    >
    > A couple things to try there, if you (the OP) really want it to be an
    > int, is to instead use intptr_t, if you have it, or uintmax_t.


    On any implementation where uintmax_t is suitable for such use,
    uintptr_t is likely (guaranteed?) to supported, so there's no point in
    worrying about uintmax_t.
     
    James Kuyper, Sep 18, 2009
    #3
  4. James Kuyper <> wrote:
    >On any implementation where uintmax_t is suitable for such use,
    >uintptr_t is likely (guaranteed?) to supported, so there's no point in
    >worrying about uintmax_t.


    uintptr_t is optional, but if it exists, the required uintmax_t can
    represent it. That's my read on the Standard, anyway (C99 7.18.1.4-5).

    I'd agree with "likely", though.

    -Beej
     
    Beej Jorgensen, Sep 18, 2009
    #4
  5. Fred

    James Kuyper Guest

    Beej Jorgensen wrote:
    > James Kuyper <> wrote:
    >> On any implementation where uintmax_t is suitable for such use,
    >> uintptr_t is likely (guaranteed?) to supported, so there's no point in
    >> worrying about uintmax_t.

    >
    > uintptr_t is optional, but if it exists, the required uintmax_t can
    > represent it. That's my read on the Standard, anyway (C99 7.18.1.4-5).
    >
    > I'd agree with "likely", though.


    I'm looking at this from the opposite direction. If uintmax_t can do the
    job, then it is a type suitable for definition as uintptr_t. There might
    be a more suitable type (smaller, faster), but you know that there's at
    least one that is suitable. It would be perverse to have a suitable
    type, and not make it available as uintptr_t; but the standard does not
    prohibit such perversions.

    This should be contrasted with the exact-width types, where if an
    implementation supports a type that meets the requirements for
    int8_t, int16_t, int32_t, or int64_t, it must define the
    corresponding typedefs.
     
    James Kuyper, Sep 18, 2009
    #5
    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. Replies:
    19
    Views:
    601
    Suman
    Aug 26, 2005
  2. HANM
    Replies:
    2
    Views:
    750
    Joseph Kesselman
    Jan 29, 2008
  3. candide
    Replies:
    65
    Views:
    1,441
  4. John Koleszar

    address of static array element as address constant

    John Koleszar, Jul 7, 2008, in forum: C Programming
    Replies:
    7
    Views:
    641
    John Koleszar
    Jul 15, 2008
  5. Vincent De Groote

    structure address = structure first field address ?

    Vincent De Groote, Apr 29, 2009, in forum: C Programming
    Replies:
    31
    Views:
    2,084
Loading...

Share This Page