OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man3 / offsetof.3
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 2014-04-06 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 offsetof \- offset of a structure member
31 .SH SYNOPSIS
32 .nf
33 .B #include <stddef.h>
34 .sp
35 .BI "size_t offsetof(" type ", " member );
36 .fi
37 .SH DESCRIPTION
38 The macro
39 .BR offsetof ()
40 returns the offset of the field
41 .I member
42 from the start of the structure
43 .IR type .
44
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
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.
51
52 A compiler error will result if
53 .I member
54 is not aligned to a byte boundary
55 (i.e., it is a bit field).
56 .SH RETURN VALUE
57 .BR offsetof ()
58 returns the offset of the given
59 .I member
60 within the given
61 .IR type ,
62 in units of bytes.
63 .SH CONFORMING TO
64 C89, C99, POSIX.1-2001.
65 .SH EXAMPLE
66 On a Linux/i386 system, when compiled using the default
67 .BR gcc (1)
68 options, the program below produces the following output:
69 .in +4n
70 .nf
71
72 .RB "$" " ./a.out"
73 offsets: i=0; c=4; d=8 a=16
74 sizeof(struct s)=16
75 .fi
76 .SS Program source
77 \&
78 .nf
79 #include <stddef.h>
80 #include <stdio.h>
81 #include <stdlib.h>
82
83 int
84 main(void)
85 {
86     struct s {
87         int i;
88         char c;
89         double d;
90         char a[];
91     };
92
93     /* Output is compiler dependent */
94
95     printf("offsets: i=%zd; c=%zd; d=%zd a=%zd\\n",
96             offsetof(struct s, i), offsetof(struct s, c),
97             offsetof(struct s, d), offsetof(struct s, a));
98     printf("sizeof(struct s)=%zd\\n", sizeof(struct s));
99
100     exit(EXIT_SUCCESS);
101 }
102 .fi
103 .SH COLOPHON
104 This page is part of release 3.65 of the Linux
105 .I man-pages
106 project.
107 A description of the project,
108 and information about reporting bugs,
109 can be found at
110 \%http://www.kernel.org/doc/man\-pages/.