offsetof
1. offsetof.3.man
Manpage of OFFSETOF
OFFSETOF
Section: Linux Programmer's Manual (3)Updated: 2008-07-12
Index Return to Main Contents
名前
offsetof - 構造体のメンバーのオフセットを返す書式
#include <stddef.h> size_t offsetof(type, member);
説明
offsetof() マクロは、フィールド member の 構造体 type の先頭からのオフセットを返す。このマクロが有用なのは、 構造体を構成するフィールドのサイズは実装によって変化するし、 コンパイラによりフィールド間に挿入するパディングのバイト数も 違う可能性があるからである。 その結果、あるエレメントのオフセットは必ずしもそれより前の エレメントのサイズの合計とはならない。
member がバイト境界に位置していない場合 (すなわち、ビットフィールドの場合) には、 コンパイラでエラーが発生する。
返り値
offsetof() は、指定された member の指定された type の中でのオフセットを、バイト単位で返す。準拠
C89, C99, POSIX.1-2001.例
Linux/i386 システムで、 gcc(1) のデフォルトオプションで コンパイルされた場合、下記のプログラムは以下のような出力を返す。$ ./a.out offsets: i=0; c=4; d=8 a=16 sizeof(struct s)=16
プログラムのソース
#include <stddef.h> #include <stdio.h> #include <stdlib.h> int main(void) { struct s { int i; char c; double d; char a[]; }; /* 出力はコンパイラ依存である */ printf("offsets: i=%ld; c=%ld; d=%ld a=%ld
", (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
", (long) sizeof(struct s)); exit(EXIT_SUCCESS); }
Index
This document was created by man2html using the manual pages.
Time: 17:39:06 GMT, May 11, 2012

