1 .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(PERMISSIVE_MISC)
5 .\" Permission is hereby granted, free of charge, to any person obtaining
6 .\" a copy of this software and associated documentation files (the
7 .\" "Software"), to deal in the Software without restriction, including
8 .\" without limitation the rights to use, copy, modify, merge, publish,
9 .\" distribute, sublicense, and/or sell copies of the Software, and to
10 .\" permit persons to whom the Software is furnished to do so, subject to
11 .\" the following conditions:
13 .\" The above copyright notice and this permission notice shall be
14 .\" included in all copies or substantial portions of the Software.
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 .\" /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h
28 .TH OFFSETOF 3 2020-11-01 "GNU" "Linux Programmer's Manual"
30 offsetof \- offset of a structure member
33 .B #include <stddef.h>
35 .BI "size_t offsetof(" type ", " member );
40 returns the offset of the field
42 from the start of the structure
45 This macro is useful because the sizes of the fields that compose
46 a structure can vary across implementations,
47 and compilers may insert different numbers of padding
49 Consequently, an element's offset is not necessarily
50 given by the sum of the sizes of the previous elements.
52 A compiler error will result if
54 is not aligned to a byte boundary
55 (i.e., it is a bit field).
58 returns the offset of the given
64 POSIX.1-2001, POSIX.1-2008, C89, C99.
66 On a Linux/i386 system, when compiled using the default
68 options, the program below produces the following output:
73 offsets: i=0; c=4; d=8 a=16
94 /* Output is compiler dependent */
96 printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\en",
97 offsetof(struct s, i), offsetof(struct s, c),
98 offsetof(struct s, d), offsetof(struct s, a));
99 printf("sizeof(struct s)=%zu\en", sizeof(struct s));