Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Address of structure element

Reply
Thread Tools

Re: Address of structure element

 
 
Fred
Guest
Posts: n/a
 
      09-17-2009
On Sep 17, 2:54*pm, Mr John FO Evans <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Beej Jorgensen
Guest
Posts: n/a
 
      09-17-2009
Fred <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      09-18-2009
Beej Jorgensen wrote:
> Fred <(E-Mail Removed)> 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.
 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      09-18-2009
James Kuyper <(E-Mail Removed)> 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

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-18-2009
Beej Jorgensen wrote:
> James Kuyper <(E-Mail Removed)> 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
[u]int8_t, [u]int16_t, [u]int32_t, or [u]int64_t, it must define the
corresponding typedefs.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structure address = structure first field address ? Vincent De Groote C Programming 31 05-19-2009 04:09 AM
address of static array element as address constant John Koleszar C Programming 7 07-15-2008 03:51 PM
Address of an array = address of its 1st element: undecidable question? candide C Programming 65 05-29-2008 08:30 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
How to change the starting address of a structure element? chee.k.cheng@gmail.com C Programming 19 08-26-2005 05:01 AM



Advertisments