man-pages/man3/offsetof.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
offsetof - offset of a structure member
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stddef.h">stddef.h</A>&gt;</B>
<B>size_t offsetof(</B><I>type</I><B>, </B><I>member</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, C89, C99.
<A NAME="lbAG">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/stddef.h">stddef.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;s&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;c;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double&nbsp;d;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;a[];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;};
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Output&nbsp;is&nbsp;compiler&nbsp;dependent&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;offsets:&nbsp;i=%zd;&nbsp;c=%zd;&nbsp;d=%zd&nbsp;a=%zd\n&quot;,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offsetof(struct&nbsp;s,&nbsp;i),&nbsp;offsetof(struct&nbsp;s,&nbsp;c),
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;offsetof(struct&nbsp;s,&nbsp;d),&nbsp;offsetof(struct&nbsp;s,&nbsp;a));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;sizeof(struct&nbsp;s)=%zd\n&quot;,&nbsp;sizeof(struct&nbsp;s));
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAI">&nbsp;</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">&nbsp;</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>