146 lines
3.8 KiB
HTML
146 lines
3.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of OFFSETOF</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>OFFSETOF</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
offsetof - offset of a structure member
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stddef.h">stddef.h</A>></B>
|
|
|
|
<B>size_t offsetof(</B><I>type</I><B>, </B><I>member</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The macro
|
|
<B>offsetof</B>()
|
|
|
|
returns the offset of the field
|
|
<I>member</I>
|
|
|
|
from the start of the structure
|
|
<I>type</I>.
|
|
|
|
<P>
|
|
|
|
This macro is useful because the sizes of the fields that compose
|
|
a structure can vary across implementations,
|
|
and compilers may insert different numbers of padding
|
|
bytes between fields.
|
|
Consequently, an element's offset is not necessarily
|
|
given by the sum of the sizes of the previous elements.
|
|
<P>
|
|
|
|
A compiler error will result if
|
|
<I>member</I>
|
|
|
|
is not aligned to a byte boundary
|
|
(i.e., it is a bit field).
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
<B>offsetof</B>()
|
|
|
|
returns the offset of the given
|
|
<I>member</I>
|
|
|
|
within the given
|
|
<I>type</I>,
|
|
|
|
in units of bytes.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, C89, C99.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
On a Linux/i386 system, when compiled using the default
|
|
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
|
|
|
|
options, the program below produces the following output:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out</B>
|
|
|
|
offsets: i=0; c=4; d=8 a=16
|
|
sizeof(struct s)=16
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/stddef.h">stddef.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(void)
|
|
{
|
|
<BR> struct s {
|
|
<BR> int i;
|
|
<BR> char c;
|
|
<BR> double d;
|
|
<BR> char a[];
|
|
<BR> };
|
|
<P>
|
|
<BR> /* Output is compiler dependent */
|
|
<P>
|
|
<BR> printf("offsets: i=%zd; c=%zd; d=%zd a=%zd\n",
|
|
<BR> offsetof(struct s, i), offsetof(struct s, c),
|
|
<BR> offsetof(struct s, d), offsetof(struct s, a));
|
|
<BR> printf("sizeof(struct s)=%zd\n", sizeof(struct s));
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">CONFORMING TO</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="7"><A HREF="#lbAH">Program source</A><DD>
|
|
</DL>
|
|
<DT id="8"><A HREF="#lbAI">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:49 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|