]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/offsetof.3
Fix inconsistencies in .TH lines
[thirdparty/man-pages.git] / man3 / offsetof.3
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
35 .BR offsetof ()
36 returns the offset of the field
37 \fImember\fP from the start of the structure \fItype\fP.
38
39 This macro is useful because the sizes of the fields that compose
40 a structure can vary across implementations,
41 and compilers may insert different numbers of padding
42 bytes between fields.
43 Consequently, an element's offset is not necessarily
44 given by the sum of the sizes of the previous elements.
45
46 A compiler error will result if
47 \fImember\fP is not aligned to a byte boundary
48 (i.e., it is a bit field).
49 .SH "RETURN VALUE"
50 .BR offsetof ()
51 returns the offset of the given element within the
52 given type, in units of bytes.
53 .SH EXAMPLE
54 On a Linux/x86 system, when compiled using the default
55 .BR gcc (1)
56 options, the program below produces the following output:
57 .fi
58
59 $ ./a.out
60 offsets: i=0; c=4; d=8 a=16
61 sizeof(struct s)=16
62
63 .nf
64 .nf
65 #include <stddef.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68
69 int
70 main(void)
71 {
72 struct s {
73 int i;
74 char c;
75 double d;
76 char a[];
77 };
78
79 /* Output is compiler dependent */
80
81 printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\\n",
82 (long) offsetof(struct s, i),
83 (long) offsetof(struct s, c),
84 (long) offsetof(struct s, d),
85 (long) offsetof(struct s, a));
86 printf("sizeof(struct s)=%ld\\n", (long) sizeof(struct s));
87
88 exit(EXIT_SUCCESS);
89 }
90 .fi
91 .SH "CONFORMING TO"
92 C89, C99, POSIX.1-2001.
93 .\" .SH SEE ALSO
94 .\" FIXME . When one day readdir_r(3) is documented, it should have
95 .\" a SEE ALSO that refers to this page.