tzfile.5, tzselect.8: sync from tzdb upstream
[man-pages.git] / man3 / offsetof.3
blob423e291eed7d027d723a44f62bbee9ff6330c35b
1 .\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
2 .\"     and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(PERMISSIVE_MISC)
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.
23 .\" %%%LICENSE_END
24 .\"
25 .\" References:
26 .\"   /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h
27 .\"   glibc-doc
28 .TH offsetof 3 (date) "Linux man-pages (unreleased)"
29 .SH NAME
30 offsetof \- offset of a structure member
31 .SH LIBRARY
32 Standard C library
33 .RI ( libc ", " \-lc )
34 .SH SYNOPSIS
35 .nf
36 .B #include <stddef.h>
37 .PP
38 .BI "size_t offsetof(" type ", " member );
39 .fi
40 .SH DESCRIPTION
41 The macro
42 .BR offsetof ()
43 returns the offset of the field
44 .I member
45 from the start of the structure
46 .IR type .
47 .PP
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
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.
54 .PP
55 A compiler error will result if
56 .I member
57 is not aligned to a byte boundary
58 (i.e., it is a bit field).
59 .SH RETURN VALUE
60 .BR offsetof ()
61 returns the offset of the given
62 .I member
63 within the given
64 .IR type ,
65 in units of bytes.
66 .SH STANDARDS
67 POSIX.1-2001, POSIX.1-2008, C99.
68 .SH EXAMPLES
69 On a Linux/i386 system, when compiled using the default
70 .BR gcc (1)
71 options, the program below produces the following output:
72 .PP
73 .in +4n
74 .EX
75 .RB "$" " ./a.out"
76 offsets: i=0; c=4; d=8 a=16
77 sizeof(struct s)=16
78 .EE
79 .in
80 .SS Program source
82 .\" SRC BEGIN (offsetof.c)
83 .EX
84 #include <stddef.h>
85 #include <stdio.h>
86 #include <stdlib.h>
88 int
89 main(void)
91     struct s {
92         int i;
93         char c;
94         double d;
95         char a[];
96     };
98     /* Output is compiler dependent */
100     printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\en",
101            offsetof(struct s, i), offsetof(struct s, c),
102            offsetof(struct s, d), offsetof(struct s, a));
103     printf("sizeof(struct s)=%zu\en", sizeof(struct s));
105     exit(EXIT_SUCCESS);
108 .\" SRC END