.\" Copyright (C) 2006 Justin Pryzby .\" and Copyright (C) 2006 Michael Kerrisk .\" .\" %%%LICENSE_START(PERMISSIVE_MISC) .\" 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. .\" %%%LICENSE_END .\" .\" References: .\" /usr/lib/gcc/i486-linux-gnu/4.1.1/include/stddef.h .\" glibc-doc .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2006-07-25, Akihiro MOTOKI, Catch up to LDP v2.34 .\" .TH OFFSETOF 3 2014\-04\-06 GNU "Linux Programmer's Manual" .SH 名前 offsetof \- 構造体のメンバーのオフセットを返す .SH 書式 .nf \fB#include \fP .sp \fBsize_t offsetof(\fP\fItype\fP\fB, \fP\fImember\fP\fB);\fP .fi .SH 説明 \fBoffsetof\fP() マクロは、フィールド \fImember\fP の 構造体 \fItype\fP の先頭からのオフセットを返す。 このマクロが有用なのは、 構造体を構成するフィールドのサイズは実装によって変化するし、 コンパイラによりフィールド間に挿入するパディングのバイト数も 違う可能性があるからである。 その結果、あるエレメントのオフセットは必ずしもそれより前の エレメントのサイズの合計とはならない。 \fImember\fP がバイト境界に位置していない場合 (すなわち、ビットフィールドの場合) には、 コンパイラでエラーが発生する。 .SH 返り値 \fBoffsetof\fP() は、指定された \fImember\fP の指定された \fItype\fP の中でのオフセットを、バイト単位で返す。 .SH 準拠 C89, C99, POSIX.1\-2001. .SH 例 Linux/i386 システムで、 \fBgcc\fP(1) のデフォルトオプションで コンパイルされた場合、下記のプログラムは以下のような出力を返す。 .in +4n .nf $\fB ./a.out\fP offsets: i=0; c=4; d=8 a=16 sizeof(struct s)=16 .fi .SS プログラムのソース \& .nf #include #include #include int main(void) { struct s { int i; char c; double d; char a[]; }; /* 出力はコンパイラ依存である */ printf("offsets: i=%zd; c=%zd; d=%zd a=%zd\en", offsetof(struct s, i), offsetof(struct s, c), offsetof(struct s, d), offsetof(struct s, a)); printf("sizeof(struct s)=%zd\en", sizeof(struct s)); exit(EXIT_SUCCESS); } .fi .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。