]>
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 | |
45186a5d | 28 | .TH OFFSETOF 3 2020-11-01 "Linux man-pages (unreleased)" |
12e41662 MK |
29 | .SH NAME |
30 | offsetof \- offset of a structure member | |
36c7aa79 AC |
31 | .SH LIBRARY |
32 | Standard C library | |
8fc3b2cf | 33 | .RI ( libc ", " \-lc ) |
12e41662 | 34 | .SH SYNOPSIS |
495846d9 MK |
35 | .nf |
36 | .B #include <stddef.h> | |
68e4db0a | 37 | .PP |
da8cb51e | 38 | .BI "size_t offsetof(" type ", " member ); |
495846d9 | 39 | .fi |
12e41662 | 40 | .SH DESCRIPTION |
60a90ecd MK |
41 | The macro |
42 | .BR offsetof () | |
43 | returns the offset of the field | |
c6fa0841 MK |
44 | .I member |
45 | from the start of the structure | |
46 | .IR type . | |
847e0d88 | 47 | .PP |
c13182ef MK |
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 | |
12e41662 MK |
51 | bytes between fields. |
52 | Consequently, an element's offset is not necessarily | |
53 | given by the sum of the sizes of the previous elements. | |
847e0d88 | 54 | .PP |
c13182ef | 55 | A compiler error will result if |
c6fa0841 MK |
56 | .I member |
57 | is not aligned to a byte boundary | |
12e41662 | 58 | (i.e., it is a bit field). |
47297adb | 59 | .SH RETURN VALUE |
60a90ecd | 60 | .BR offsetof () |
d7e02b73 MK |
61 | returns the offset of the given |
62 | .I member | |
63 | within the given | |
64 | .IR type , | |
65 | in units of bytes. | |
3113c7f3 | 66 | .SH STANDARDS |
bbe0931d | 67 | POSIX.1-2001, POSIX.1-2008, C89, C99. |
a14af333 | 68 | .SH EXAMPLES |
34ccb744 | 69 | On a Linux/i386 system, when compiled using the default |
60a90ecd | 70 | .BR gcc (1) |
e15f6322 | 71 | options, the program below produces the following output: |
e646a1ba | 72 | .PP |
624c0456 | 73 | .in +4n |
e646a1ba | 74 | .EX |
b43a3b30 | 75 | .RB "$" " ./a.out" |
624c0456 MK |
76 | offsets: i=0; c=4; d=8 a=16 |
77 | sizeof(struct s)=16 | |
b9c93deb MK |
78 | .EE |
79 | .in | |
9c330504 | 80 | .SS Program source |
d84d0300 | 81 | \& |
e7d0bb47 | 82 | .EX |
12e41662 MK |
83 | #include <stddef.h> |
84 | #include <stdio.h> | |
85 | #include <stdlib.h> | |
86 | ||
c13182ef | 87 | int |
cf0a9ace | 88 | main(void) |
12e41662 MK |
89 | { |
90 | struct s { | |
91 | int i; | |
92 | char c; | |
93 | double d; | |
e15f6322 | 94 | char a[]; |
12e41662 MK |
95 | }; |
96 | ||
e15f6322 | 97 | /* Output is compiler dependent */ |
c13182ef | 98 | |
2bbfed1b | 99 | printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\en", |
8c5fcd21 MK |
100 | offsetof(struct s, i), offsetof(struct s, c), |
101 | offsetof(struct s, d), offsetof(struct s, a)); | |
2bbfed1b | 102 | printf("sizeof(struct s)=%zu\en", sizeof(struct s)); |
c13182ef | 103 | |
12e41662 MK |
104 | exit(EXIT_SUCCESS); |
105 | } | |
e7d0bb47 | 106 | .EE |