1OFFSETOF(3) Linux Programmer's Manual OFFSETOF(3)
2
3
4
6 offsetof - offset of a structure member
7
9 #include <stddef.h>
10
11 size_t offsetof(type, member);
12
14 The macro offsetof() returns the offset of the field member from the
15 start of the structure type.
16
17 This macro is useful because the sizes of the fields that compose a
18 structure can vary across implementations, and compilers may insert
19 different numbers of padding bytes between fields. Consequently, an
20 element's offset is not necessarily given by the sum of the sizes of
21 the previous elements.
22
23 A compiler error will result if member is not aligned to a byte bound‐
24 ary (i.e., it is a bit field).
25
27 offsetof() returns the offset of the given member within the given
28 type, in units of bytes.
29
31 POSIX.1-2001, POSIX.1-2008, C89, C99.
32
34 On a Linux/i386 system, when compiled using the default gcc(1) options,
35 the program below produces the following output:
36
37 $ ./a.out
38 offsets: i=0; c=4; d=8 a=16
39 sizeof(struct s)=16
40
41 Program source
42
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46
47 int
48 main(void)
49 {
50 struct s {
51 int i;
52 char c;
53 double d;
54 char a[];
55 };
56
57 /* Output is compiler dependent */
58
59 printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\n",
60 offsetof(struct s, i), offsetof(struct s, c),
61 offsetof(struct s, d), offsetof(struct s, a));
62 printf("sizeof(struct s)=%zu\n", sizeof(struct s));
63
64 exit(EXIT_SUCCESS);
65 }
66
68 This page is part of release 5.10 of the Linux man-pages project. A
69 description of the project, information about reporting bugs, and the
70 latest version of this page, can be found at
71 https://www.kernel.org/doc/man-pages/.
72
73
74
75GNU 2020-11-01 OFFSETOF(3)