The idea is that it is possible to make a pointer to an instance of Struct_A or
an instance of Struct_B with a pointer of the BaseStruct type.
Can I create a "base struct-type" in C as shown below with any compiler?
I tested it with GCC and it worked, but I am not sure if it would work everywhere ...
#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");
}