| 1 |
.\" Copyright 2006, Justin Pryzby <[email protected]> |
| 2 |
.\" Copyright 2006, Michael Kerrisk <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" %%%LICENSE_START(PERMISSIVE_MISC) |
| 6 |
.\" Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
.\" a copy of this software and associated documentation files (the |
| 8 |
.\" "Software"), to deal in the Software without restriction, including |
| 9 |
.\" without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
.\" distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
.\" permit persons to whom the Software is furnished to do so, subject to |
| 12 |
.\" the following conditions: |
| 13 |
.\" |
| 14 |
.\" The above copyright notice and this permission notice shall be |
| 15 |
.\" included in all copies or substantial portions of the Software. |
| 16 |
.\" |
| 17 |
.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
.\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
.\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
.\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
.\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
.\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
.\" %%%LICENSE_END |
| 25 |
.\" |
| 26 |
.TH offsetof 3 2026-02-08 "Linux man-pages 6.18" |
| 27 |
.SH NAME |
| 28 |
offsetof \- offset of a structure member |
| 29 |
.SH LIBRARY |
| 30 |
Standard C library |
| 31 |
.RI ( libc ,\~ \-lc ) |
| 32 |
.SH SYNOPSIS |
| 33 |
.nf |
| 34 |
.B #include <stddef.h> |
| 35 |
.P |
| 36 |
.BI "size_t offsetof(" type ", " member ); |
| 37 |
.fi |
| 38 |
.SH DESCRIPTION |
| 39 |
The macro |
| 40 |
.BR offsetof () |
| 41 |
returns the offset of the field |
| 42 |
.I member |
| 43 |
from the start of the structure |
| 44 |
.IR type . |
| 45 |
.P |
| 46 |
This macro is useful because the sizes of the fields that compose |
| 47 |
a structure can vary across implementations, |
| 48 |
and compilers may insert different numbers of padding |
| 49 |
bytes between fields. |
| 50 |
Consequently, an element's offset is not necessarily |
| 51 |
given by the sum of the sizes of the previous elements. |
| 52 |
.P |
| 53 |
A compiler error will result if |
| 54 |
.I member |
| 55 |
is not aligned to a byte boundary |
| 56 |
(i.e., it is a bit field). |
| 57 |
.SH RETURN VALUE |
| 58 |
.BR offsetof () |
| 59 |
returns the offset of the given |
| 60 |
.I member |
| 61 |
within the given |
| 62 |
.IR type , |
| 63 |
in units of bytes. |
| 64 |
.SH STANDARDS |
| 65 |
C11, POSIX.1-2008. |
| 66 |
.SH HISTORY |
| 67 |
POSIX.1-2001, C89. |
| 68 |
.SH EXAMPLES |
| 69 |
On a Linux/i386 system, when compiled using the default |
| 70 |
.BR gcc (1) |
| 71 |
options, the program below produces the following output: |
| 72 |
.P |
| 73 |
.in +4n |
| 74 |
.EX |
| 75 |
.RB "$" " ./a.out" |
| 76 |
offsets: i=0; c=4; d=8 a=16 |
| 77 |
sizeof(struct s)=16 |
| 78 |
.EE |
| 79 |
.in |
| 80 |
.SS Program source |
| 81 |
\& |
| 82 |
.\" SRC BEGIN (offsetof.c) |
| 83 |
.EX |
| 84 |
#include <stddef.h> |
| 85 |
#include <stdio.h> |
| 86 |
#include <stdlib.h> |
| 87 |
\& |
| 88 |
int |
| 89 |
main(void) |
| 90 |
{ |
| 91 |
struct s { |
| 92 |
int i; |
| 93 |
char c; |
| 94 |
double d; |
| 95 |
char a[]; |
| 96 |
}; |
| 97 |
\& |
| 98 |
/* Output is compiler dependent */ |
| 99 |
\& |
| 100 |
printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\[rs]n", |
| 101 |
offsetof(struct s, i), offsetof(struct s, c), |
| 102 |
offsetof(struct s, d), offsetof(struct s, a)); |
| 103 |
printf("sizeof(struct s)=%zu\[rs]n", sizeof(struct s)); |
| 104 |
\& |
| 105 |
exit(EXIT_SUCCESS); |
| 106 |
} |
| 107 |
.EE |
| 108 |
.\" SRC END |