xref: /netbsd-src/share/man/man3/offsetof.3 (revision 716425e6ac5e8a54bdd8556fde4c7cd98ae2cb54)
1.\"	$NetBSD: offsetof.3,v 1.5 2011/04/14 06:56:28 wiz Exp $
2.\"
3.\"	$OpenBSD: offsetof.3,v 1.2 2010/02/18 18:30:19 jmc Exp $
4.\"
5.\" Copyright (c) 2010 Thomas Pfaff <tpfaff@tp76.info>
6.\"
7.\" Permission to use, copy, modify, and distribute this software for any
8.\" purpose with or without fee is hereby granted, provided that the above
9.\" copyright notice and this permission notice appear in all copies.
10.\"
11.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18.\"
19.\"
20.Dd April 1, 2011
21.Dt OFFSETOF 3
22.Os
23.Sh NAME
24.Nm offsetof
25.Nd offset of a structure member
26.Sh SYNOPSIS
27.In stddef.h
28.Ft size_t
29.Fn offsetof "type" "member"
30.Sh DESCRIPTION
31The
32.Fn offsetof
33macro expands to an integer constant expression of type
34.Ft size_t
35and yields the offset,
36in bytes, of the field
37.Ar member
38from the start of the structure
39.Ar type .
40.Pp
41A compiler error will result if
42.Ar member
43is not aligned to a byte boundary (i.e. it is a bit-field).
44.Sh EXAMPLES
45Regardless of the architecture and the
46.Tn ABI ,
47the following example prints the value zero for the variable
48.Va x .
49.Bd -literal -offset indent
50struct example {
51	double	x;
52	int	y;
53	char	z;
54};
55
56size_t x, y, z;
57
58x = offsetof(struct example, x);
59y = offsetof(struct example, y);
60z = offsetof(struct example, z);
61
62(void)printf("%zu %zu %zu\en", x, y, z);
63.Ed
64.Sh SEE ALSO
65.Xr __alignof__ 3 ,
66.Xr stddef 3 ,
67.Xr typeof 3
68.Sh STANDARDS
69The
70.Fn offsetof
71macro conforms to
72.St -ansiC .
73