Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
04/21/03 00:04
Read: times


 
#43820 - RE: Pointers to Structure Elements
Responding to: ???'s previous message
Here is the answer (from the book the Indispensable guide to C)

the Macro offsetof may be used to determine the offset, in bytes, of a structures member elements relative to the address of the structure itself, and yields a result of type size_t. The first argument to offsetof() must be the structure template's name, not the name of any structure variable based upon it. The second argument is the name of the member element within the structure.

That being said here is the macro.
#define offsetof(s_type, member) (size_t)&(((s_type*)0)->member)

And here is how I used it (for testing)

printf("Element DBActions.Date is %u bytes from start of Structn",
(unsigned int)offsetof(struct Action,StartTime));

if you refer to the first post on this subject the structure is define there, and the Date is the 6th byte from the start of the structure which is the result that this macro posted.

Thanks for the assistance


Regards
Marshall Brown


List of 12 messages in thread
TopicAuthorDate
Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
      RE: Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
      RE: Pointers to Structure Elements            01/01/70 00:00      
         RE: Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
   RE: Pointers to Structure Elements            01/01/70 00:00      
   Universal casting to structs/union/void            01/01/70 00:00      
      RE: Universal casting to structs/union/void            01/01/70 00:00      

Back to Subject List