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 (date) "Linux man-pages (unreleased)"
30 offsetof \- offset of a structure member
33 .RI ( libc ", " \-lc )
36 .B #include <stddef.h>
38 .BI "size_t offsetof(" type ", " member );
43 returns the offset of the field
45 from the start of the structure
48 This macro is useful because the sizes of the fields that compose
49 a structure can vary across implementations,
50 and compilers may insert different numbers of padding
52 Consequently, an element's offset is not necessarily
53 given by the sum of the sizes of the previous elements.
55 A compiler error will result if
57 is not aligned to a byte boundary
58 (i.e., it is a bit field).
61 returns the offset of the given
67 POSIX.1-2001, POSIX.1-2008, C99.
69 On a Linux/i386 system, when compiled using the default
71 options, the program below produces the following output:
76 offsets: i=0; c=4; d=8 a=16
82 .\" SRC BEGIN (offsetof.c)
98 /* Output is compiler dependent */
100 printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\en",
101 offsetof(struct s, i), offsetof(struct s, c),
102 offsetof(struct s, d), offsetof(struct s, a));
103 printf("sizeof(struct s)=%zu\en", sizeof(struct s));