0
のアイデアは、BaseStructタイプのポインタでStruct_A又は
のインスタンスStruct_Bのインスタンスへのポインタを作ることが可能であるということです。Cプログラミング - ベース構造体型「派生」タイプ
コンパイラで以下のようにCで "base struct-type"を作成できますか?
あなたの質問に答えるために、まず
#include <stdio.h>
typedef enum {
StructType_A,
StructType_B
} StructType;
// >>> base type <<<
typedef struct {
StructType type;
} BaseStruct;
// >>> type A <<<
typedef struct {
StructType type;
int xyz;
char blabliblub;
} Struct_A;
// >>> type B <<<
typedef struct {
StructType type;
long int abc;
} Struct_B;
int main() {
Struct_A a;
BaseStruct* ptr;
a.type = StructType_A;
a.xyz = 7853;
a.blabliblub = 'z';
ptr = (BaseStruct*) &a;
if (ptr->type == StructType_A) printf ("YAY :3\n");
}
をあなたの質問はC程度であれば、しないクロスタグC++(C++ Cに関連するものがある場合を除き! = C++)。 – crashmstr
より一般的な属性で後で終了する可能性があるため、派生型の最初の属性として基本構造体を持つ方が良い –
基本型は、データを格納するために使用されません。タイプAまたはタイプBにしかアクセスできない – ikkowy