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