コンパイル時に構造体のメンバーのオフセットを見つける方法はありますか? 構造体メンバーのオフセットを含む定数を作成したいと考えています。次のコードでは、offsetof()
マクロは最初のprintf
ステートメントで機能します。ただし、10 行目で宣言するために使用するとofs
、エラーが発生します。
「'->' 演算子を定数式として解決できません」.
それを行う他の方法はありますか?
struct MyStruct
{
unsigned long lw;
unsigned char c[5];
int i;
int j;
unsigned long last;
};
const int ofs = offsetof(struct MyStruct, i); // This line in error
int main(void)
{
printf("Offset of c = %d.\n", offsetof(struct MyStruct, c) );
printf("Offset of i = %d.\n", ofs );
return 0;
}