This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
offsetof
Prototype
#include <stddef.h>
size_t offsetof(type, member-designator)
Description
The offsetof
macro calculates the offset in bytes of the
structure member member-designator from the beginning
of the structure type. The member should not be a bit-field.
Example
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/*
* This example show the alignment of the structure members done by
* the compiler.
*/
struct s {
char member1;
int member2;
};
int main(void)
{
printf("offsetof(struct s, member1) = %d\n",
offsetof(struct s, member1));
printf("offsetof(struct s, member2) = %d\n",
offsetof(struct s, member2));
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.1.6