]>
Commit | Line | Data |
---|---|---|
12e41662 | 1 | .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com> |
c11b1abf | 2 | .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com> |
12e41662 | 3 | .\" |
7c576f45 | 4 | .\" %%%LICENSE_START(PERMISSIVE_MISC) |
12e41662 MK |
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: | |
12 | .\" | |
13 | .\" The above copyright notice and this permission notice shall be | |
14 | .\" included in all copies or substantial portions of the Software. | |
15 | .\" | |
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. | |
8ff7380d | 23 | .\" %%%LICENSE_END |
12e41662 MK |
24 | .\" |
25 | .\" References: | |
26 | .\" /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h | |
27 | .\" glibc-doc | |
9ba01802 | 28 | .TH OFFSETOF 3 2019-03-06 "GNU" "Linux Programmer's Manual" |
12e41662 MK |
29 | .SH NAME |
30 | offsetof \- offset of a structure member | |
31 | .SH SYNOPSIS | |
495846d9 MK |
32 | .nf |
33 | .B #include <stddef.h> | |
68e4db0a | 34 | .PP |
da8cb51e | 35 | .BI "size_t offsetof(" type ", " member ); |
495846d9 | 36 | .fi |
12e41662 | 37 | .SH DESCRIPTION |
60a90ecd MK |
38 | The macro |
39 | .BR offsetof () | |
40 | returns the offset of the field | |
c6fa0841 MK |
41 | .I member |
42 | from the start of the structure | |
43 | .IR type . | |
847e0d88 | 44 | .PP |
c13182ef MK |
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 | |
12e41662 MK |
48 | bytes between fields. |
49 | Consequently, an element's offset is not necessarily | |
50 | given by the sum of the sizes of the previous elements. | |
847e0d88 | 51 | .PP |
c13182ef | 52 | A compiler error will result if |
c6fa0841 MK |
53 | .I member |
54 | is not aligned to a byte boundary | |
12e41662 | 55 | (i.e., it is a bit field). |
47297adb | 56 | .SH RETURN VALUE |
60a90ecd | 57 | .BR offsetof () |
d7e02b73 MK |
58 | returns the offset of the given |
59 | .I member | |
60 | within the given | |
61 | .IR type , | |
62 | in units of bytes. | |
47297adb | 63 | .SH CONFORMING TO |
bbe0931d | 64 | POSIX.1-2001, POSIX.1-2008, C89, C99. |
12e41662 | 65 | .SH EXAMPLE |
34ccb744 | 66 | On a Linux/i386 system, when compiled using the default |
60a90ecd | 67 | .BR gcc (1) |
e15f6322 | 68 | options, the program below produces the following output: |
e646a1ba | 69 | .PP |
624c0456 | 70 | .in +4n |
e646a1ba | 71 | .EX |
b43a3b30 | 72 | .RB "$" " ./a.out" |
624c0456 MK |
73 | offsets: i=0; c=4; d=8 a=16 |
74 | sizeof(struct s)=16 | |
b9c93deb MK |
75 | .EE |
76 | .in | |
9c330504 | 77 | .SS Program source |
d84d0300 | 78 | \& |
e7d0bb47 | 79 | .EX |
12e41662 MK |
80 | #include <stddef.h> |
81 | #include <stdio.h> | |
82 | #include <stdlib.h> | |
83 | ||
c13182ef | 84 | int |
cf0a9ace | 85 | main(void) |
12e41662 MK |
86 | { |
87 | struct s { | |
88 | int i; | |
89 | char c; | |
90 | double d; | |
e15f6322 | 91 | char a[]; |
12e41662 MK |
92 | }; |
93 | ||
e15f6322 | 94 | /* Output is compiler dependent */ |
c13182ef | 95 | |
d1a71985 | 96 | printf("offsets: i=%zd; c=%zd; d=%zd a=%zd\en", |
8c5fcd21 MK |
97 | offsetof(struct s, i), offsetof(struct s, c), |
98 | offsetof(struct s, d), offsetof(struct s, a)); | |
d1a71985 | 99 | printf("sizeof(struct s)=%zd\en", sizeof(struct s)); |
c13182ef | 100 | |
12e41662 MK |
101 | exit(EXIT_SUCCESS); |
102 | } | |
e7d0bb47 | 103 | .EE |