]>
Commit | Line | Data |
---|---|---|
12e41662 MK |
1 | .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com> |
2 | .\" and Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net> | |
3 | .\" | |
4 | .\" Permission is hereby granted, free of charge, to any person obtaining | |
5 | .\" a copy of this software and associated documentation files (the | |
6 | .\" "Software"), to deal in the Software without restriction, including | |
7 | .\" without limitation the rights to use, copy, modify, merge, publish, | |
8 | .\" distribute, sublicense, and/or sell copies of the Software, and to | |
9 | .\" permit persons to whom the Software is furnished to do so, subject to | |
10 | .\" the following conditions: | |
11 | .\" | |
12 | .\" The above copyright notice and this permission notice shall be | |
13 | .\" included in all copies or substantial portions of the Software. | |
14 | .\" | |
15 | .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 | .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 | .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 | .\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 | .\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 | .\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 | .\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 | .\" | |
23 | .\" References: | |
24 | .\" /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h | |
25 | .\" glibc-doc | |
26 | .TH OFFSETOF 3 "2006-05-23" GNU "Linux Programmer's Manual" | |
27 | .SH NAME | |
28 | offsetof \- offset of a structure member | |
29 | .SH SYNOPSIS | |
30 | \fB#include <stddef.h> | |
31 | ||
32 | \fBsize_t offsetof(\fItype\fP, \fPmember\fP); | |
33 | .SH DESCRIPTION | |
34 | The macro \fBoffsetof\fP() returns the offset of the field | |
35 | \fImember\fP from the start of the structure \fItype\fP. | |
36 | ||
37 | This macro is useful because the sizes of the fields that compose | |
38 | a structure can vary across implementations, | |
39 | and compilers may insert different numbers of padding | |
40 | bytes between fields. | |
41 | Consequently, an element's offset is not necessarily | |
42 | given by the sum of the sizes of the previous elements. | |
43 | ||
44 | A compiler error will result if | |
45 | \fImember\fP is not aligned to a byte boundary | |
46 | (i.e., it is a bit field). | |
47 | .SH "RETURN VALUE" | |
48 | \fBoffsetof\fP() returns the offset of the given element within the | |
49 | given type, in units of bytes. | |
50 | .SH EXAMPLE | |
51 | .nf | |
52 | .fi | |
53 | #include <stddef.h> | |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
56 | ||
57 | int main() | |
58 | { | |
59 | struct s { | |
60 | int i; | |
61 | char c; | |
62 | double d; | |
63 | char a[]; | |
64 | }; | |
65 | ||
66 | /* Output is compiler-dependent */ | |
67 | printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\\n", | |
68 | (long) offsetof(struct s, i), | |
69 | (long) offsetof(struct s, c), | |
70 | (long) offsetof(struct s, d), | |
71 | (long) offsetof(struct s, a)); | |
72 | printf("sizeof(struct s)=%ld\\n", (long) sizeof(struct s)); | |
73 | exit(EXIT_SUCCESS); | |
74 | } | |
75 | .\" .SH NOTES | |
76 | .\" \fBoffsetof\fP can be implemented as: | |
77 | .\" .sp | |
78 | .\" .nf | |
79 | .\" \fB#define offsetof(\fItype\fP, \fPmember\fP) \\ | |
80 | .\" \fB&((type *)0)->\fImember\fP - (char *)((\fPtype\fP *)0) | |
81 | .\" .fi | |
82 | .SH "CONFORMING TO" | |
83 | POSIX.1-2001. | |
84 | .\" .SH SEE ALSO | |
85 | .\" FIXME . When one day readdir_r(3) is documented, it should have | |
86 | .\" a SEE ALSO that refers to this page. |