名前空間
変種

offsetof

提供: cppreference.com
< c | types
<tbody> </tbody>
ヘッダ <stddef.h> で定義
#define offsetof(type, member) /*implementation-defined*/

マクロ offsetof は、指定された型のオブジェクトの先頭から指定されたメンバまでの、パディング (もしあれば) を含むバイト単位のオフセットを値に持つ、 size_t 型の整数定数式に展開されます。

#include <stdio.h>
#include <stddef.h>

struct S {
    char c;
    double d;
};

int main(void)
{
    printf("the first element is at offset %zu\n", offsetof(struct S, c));
    printf("the double is at offset %zu\n", offsetof(struct S, d));
}

出力例:

the first element is at offset 0
the double is at offset 8

関連項目

sizeof 演算子の結果の符号なし整数型
(typedef) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.