1offsetof(3)                Library Functions Manual                offsetof(3)
2
3
4

NAME

6       offsetof - offset of a structure member
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stddef.h>
13
14       size_t offsetof(type, member);
15

DESCRIPTION

17       The  macro  offsetof()  returns the offset of the field member from the
18       start of the structure type.
19
20       This macro is useful because the sizes of the  fields  that  compose  a
21       structure  can  vary  across  implementations, and compilers may insert
22       different numbers of padding bytes between  fields.   Consequently,  an
23       element's  offset  is  not necessarily given by the sum of the sizes of
24       the previous elements.
25
26       A compiler error will result if member is not aligned to a byte  bound‐
27       ary (i.e., it is a bit field).
28

RETURN VALUE

30       offsetof()  returns  the  offset  of  the given member within the given
31       type, in units of bytes.
32

STANDARDS

34       C11, POSIX.1-2008.
35

HISTORY

37       POSIX.1-2001, C89.
38

EXAMPLES

40       On a Linux/i386 system, when compiled using the default gcc(1) options,
41       the program below produces the following output:
42
43           $ ./a.out
44           offsets: i=0; c=4; d=8 a=16
45           sizeof(struct s)=16
46
47   Program source
48
49       #include <stddef.h>
50       #include <stdio.h>
51       #include <stdlib.h>
52
53       int
54       main(void)
55       {
56           struct s {
57               int i;
58               char c;
59               double d;
60               char a[];
61           };
62
63           /* Output is compiler dependent */
64
65           printf("offsets: i=%zu; c=%zu; d=%zu a=%zu\n",
66                  offsetof(struct s, i), offsetof(struct s, c),
67                  offsetof(struct s, d), offsetof(struct s, a));
68           printf("sizeof(struct s)=%zu\n", sizeof(struct s));
69
70           exit(EXIT_SUCCESS);
71       }
72
73
74
75Linux man-pages 6.05              2023-05-03                       offsetof(3)
Impressum