I have the following working code.
#include <stdlib.h>
#include <stdio.h>
typedef struct Mystruct {
int id;
struct Mystruct** mystructures;
} Mystruct;
int main () {
Mystruct* ms1 = malloc(sizeof(Mystruct));
ms1->id = 1;
Mystruct* ms2 = malloc(sizeof(Mystruct));
ms2->id = 2;
Mystruct* ms3 = malloc(sizeof(Mystruct));
ms3->id = 3;
ms1->mystructures = malloc(sizeof(Mystruct) * 2);
ms1->mystructures[0] = (struct Mystruct*)ms2;
ms1->mystructures[1] = (struct Mystruct*)ms3;
printf("%d\n", ms1->mystructures[0]->id);
}
I don't like having to use both Mystruct
and (struct Mystruct)
to refer to the same data type. Is there a way to use typedefined Mystruct recursively like in
typedef struct {
int id;
Mystruct** mystructures;
} Mystruct;
? This doesn't compile. Thanks