[Previous]
[Contents]
[Next]

offsetof()

return the offset of an element in a structure

Synopsis:

#include <stddef.h>
size_t offsetof( composite, name );

Description:

The offsetof() macro returns the offset of the element name within the struct or union composite. This provides a portable method to determine the offset.

Returns:

The offset of name.

Examples:

#include <stdio.h>
#include <stddef.h>

struct new_def
{  char *first;
   char second[10];
   int third;
};

void main()
  {
    printf( "first:%d second:%d third:%d\n",
    offsetof( struct new_def, first ),
    offsetof( struct new_def, second ),
    offsetof( struct new_def, third ) );
  }

In a small data model, the following would result:

first:0 second:2 third:12

In a large data model, the following would result:

first:0 second:4 third:14

Classification:

ANSI

Safety:
Interrupt handler Yes
Signal handler Yes
Thread Yes

Caveats:

offsetof() is a macro.


[Previous]
[Contents]
[Next]