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