]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/offsetof.3
locale.1, pldd.1, bpf.2, clone.2, copy_file_range.2, dup.2, execve.2, futex.2, get_ke...
[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
460495ca 28.TH OFFSETOF 3 2015-08-08 "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>
68e4db0a 34.PP
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
c6fa0841
MK
41.I member
42from the start of the structure
43.IR type .
847e0d88 44.PP
c13182ef
MK
45This macro is useful because the sizes of the fields that compose
46a structure can vary across implementations,
47and compilers may insert different numbers of padding
12e41662
MK
48bytes between fields.
49Consequently, an element's offset is not necessarily
50given by the sum of the sizes of the previous elements.
847e0d88 51.PP
c13182ef 52A compiler error will result if
c6fa0841
MK
53.I member
54is not aligned to a byte boundary
12e41662 55(i.e., it is a bit field).
47297adb 56.SH RETURN VALUE
60a90ecd 57.BR offsetof ()
d7e02b73
MK
58returns the offset of the given
59.I member
60within the given
61.IR type ,
62in units of bytes.
47297adb 63.SH CONFORMING TO
bbe0931d 64POSIX.1-2001, POSIX.1-2008, C89, C99.
12e41662 65.SH EXAMPLE
34ccb744 66On a Linux/i386 system, when compiled using the default
60a90ecd 67.BR gcc (1)
e15f6322 68options, the program below produces the following output:
624c0456 69.in +4n
c8250206 70.nf
e15f6322 71
b43a3b30 72.RB "$" " ./a.out"
624c0456
MK
73offsets: i=0; c=4; d=8 a=16
74sizeof(struct s)=16
c8250206 75.fi
9c330504 76.SS Program source
d84d0300 77\&
e7d0bb47 78.EX
12e41662
MK
79#include <stddef.h>
80#include <stdio.h>
81#include <stdlib.h>
82
c13182ef 83int
cf0a9ace 84main(void)
12e41662
MK
85{
86 struct s {
87 int i;
88 char c;
89 double d;
e15f6322 90 char a[];
12e41662
MK
91 };
92
e15f6322 93 /* Output is compiler dependent */
c13182ef 94
8c5fcd21
MK
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));
c13182ef 99
12e41662
MK
100 exit(EXIT_SUCCESS);
101}
e7d0bb47 102.EE