]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/offsetof.3
sched_setaffinity.3: minor: rework EPERM text
[thirdparty/man-pages.git] / man3 / offsetof.3
CommitLineData
12e41662 1.\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
c11b1abf 2.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
12e41662
MK
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
d7e02b73 26.TH OFFSETOF 3 2008-07-12 "GNU" "Linux Programmer's Manual"
12e41662
MK
27.SH NAME
28offsetof \- offset of a structure member
29.SH SYNOPSIS
495846d9
MK
30.nf
31.B #include <stddef.h>
32.sp
da8cb51e 33.BI "size_t offsetof(" type ", " member );
495846d9 34.fi
12e41662 35.SH DESCRIPTION
60a90ecd
MK
36The macro
37.BR offsetof ()
38returns the offset of the field
12e41662
MK
39\fImember\fP from the start of the structure \fItype\fP.
40
c13182ef
MK
41This macro is useful because the sizes of the fields that compose
42a structure can vary across implementations,
43and compilers may insert different numbers of padding
12e41662
MK
44bytes between fields.
45Consequently, an element's offset is not necessarily
46given by the sum of the sizes of the previous elements.
47
c13182ef
MK
48A compiler error will result if
49\fImember\fP is not aligned to a byte boundary
12e41662
MK
50(i.e., it is a bit field).
51.SH "RETURN VALUE"
60a90ecd 52.BR offsetof ()
d7e02b73
MK
53returns the offset of the given
54.I member
55within the given
56.IR type ,
57in units of bytes.
2b2581ee
MK
58.SH "CONFORMING TO"
59C89, C99, POSIX.1-2001.
12e41662 60.SH EXAMPLE
34ccb744 61On a Linux/i386 system, when compiled using the default
60a90ecd 62.BR gcc (1)
e15f6322 63options, the program below produces the following output:
624c0456 64.in +4n
c8250206 65.nf
e15f6322 66
b43a3b30 67.RB "$" " ./a.out"
624c0456
MK
68offsets: i=0; c=4; d=8 a=16
69sizeof(struct s)=16
c8250206 70.fi
e15f6322 71.nf
9c330504
MK
72.SS Program source
73.R " "
624c0456 74.nf
12e41662
MK
75#include <stddef.h>
76#include <stdio.h>
77#include <stdlib.h>
78
c13182ef 79int
cf0a9ace 80main(void)
12e41662
MK
81{
82 struct s {
83 int i;
84 char c;
85 double d;
e15f6322 86 char a[];
12e41662
MK
87 };
88
e15f6322 89 /* Output is compiler dependent */
c13182ef 90
12e41662 91 printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\\n",
c13182ef 92 (long) offsetof(struct s, i),
e15f6322 93 (long) offsetof(struct s, c),
c13182ef 94 (long) offsetof(struct s, d),
e15f6322 95 (long) offsetof(struct s, a));
12e41662 96 printf("sizeof(struct s)=%ld\\n", (long) sizeof(struct s));
c13182ef 97
12e41662
MK
98 exit(EXIT_SUCCESS);
99}
e15f6322 100.fi
12e41662 101.\" .SH SEE ALSO
c13182ef 102.\" FIXME . When one day readdir_r(3) is documented, it should have
12e41662 103.\" a SEE ALSO that refers to this page.