[Previous] [Contents] [Next]

offsetof()

Return the offset of a structure element

Synopsis:

#include <stddef.h>

#define offsetof( composite, name ) ...

Library:

libc

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 <stdlib.h>
#include <stddef.h>

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

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

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes

Caveats:

offsetof() is a macro.


[Previous] [Contents] [Next]