New page for offsetof()

This commit is contained in:
Michael Kerrisk 2006-05-23 21:47:35 +00:00
parent c98c9784f6
commit 12e416621b
1 changed files with 86 additions and 0 deletions

86
man3/offsetof.3 Normal file
View File

@ -0,0 +1,86 @@
.\" Copyright (C) 2006 Justin Pryzby <pryzbyj@justinpryzby.com>
.\" and Copyright (C) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
.\"
.\" Permission is hereby granted, free of charge, to any person obtaining
.\" a copy of this software and associated documentation files (the
.\" "Software"), to deal in the Software without restriction, including
.\" without limitation the rights to use, copy, modify, merge, publish,
.\" distribute, sublicense, and/or sell copies of the Software, and to
.\" permit persons to whom the Software is furnished to do so, subject to
.\" the following conditions:
.\"
.\" The above copyright notice and this permission notice shall be
.\" included in all copies or substantial portions of the Software.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
.\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
.\" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
.\" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
.\" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
.\" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.\"
.\" References:
.\" /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h
.\" glibc-doc
.TH OFFSETOF 3 "2006-05-23" GNU "Linux Programmer's Manual"
.SH NAME
offsetof \- offset of a structure member
.SH SYNOPSIS
\fB#include <stddef.h>
\fBsize_t offsetof(\fItype\fP, \fPmember\fP);
.SH DESCRIPTION
The macro \fBoffsetof\fP() returns the offset of the field
\fImember\fP from the start of the structure \fItype\fP.
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.
A compiler error will result if
\fImember\fP is not aligned to a byte boundary
(i.e., it is a bit field).
.SH "RETURN VALUE"
\fBoffsetof\fP() returns the offset of the given element within the
given type, in units of bytes.
.SH EXAMPLE
.nf
.fi
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct s {
int i;
char c;
double d;
char a[];
};
/* Output is compiler-dependent */
printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\\n",
(long) offsetof(struct s, i),
(long) offsetof(struct s, c),
(long) offsetof(struct s, d),
(long) offsetof(struct s, a));
printf("sizeof(struct s)=%ld\\n", (long) sizeof(struct s));
exit(EXIT_SUCCESS);
}
.\" .SH NOTES
.\" \fBoffsetof\fP can be implemented as:
.\" .sp
.\" .nf
.\" \fB#define offsetof(\fItype\fP, \fPmember\fP) \\
.\" \fB&((type *)0)->\fImember\fP - (char *)((\fPtype\fP *)0)
.\" .fi
.SH "CONFORMING TO"
POSIX.1-2001.
.\" .SH SEE ALSO
.\" FIXME . When one day readdir_r(3) is documented, it should have
.\" a SEE ALSO that refers to this page.