type フィールド定義を持つ 2 つの異なる構造があります (以下を参照してください)。
struct A {
int type;
char type_a[8];
char random[8];
};
struct B {
int type;
char type_b[16];
char random[16];
};
ここで、タイプに基づいてこの 2 つの構造を区別したいので、たとえば
if (type == A)
struct A *a = (struct A *)buff;
if (type == B)
struct B *b = (struct B *)buff;
事前にバフで渡される構造の種類はわかりません。では、バフからタイプを抽出するにはどうすればよいですか。type フィールドは、両方の構造体の最初のフィールドであることが保証されています。