I am not the greatest C programmer who ever lived so this might be a silly question but is there a way for all types of a specific struct to reference the same struct instance of another struct?
An example of this would be:
#include <stdio.h>
#include <stdlib.h>
int idxgiver;
typedef struct component component_t;
typedef struct componentA componentA_t;
typedef struct componentB componentB_t;
static struct component{
int idx;
};
struct componentA{
component_t component;
};
struct componentB{
component_t component;
};
componentA_t *componentA_init(){
componentA_t *a = malloc(sizeof(componentA_t));
if(a->component.idx == 0){
a->component.idx = idxgiver;
idxgiver++;
}
return a;
}
componentB_t *componentB_init(){
componentB_t *b = malloc(sizeof(componentB_t));
if(b->component.idx == 0){
b->component.idx = idxgiver;
idxgiver++;
}
return b;
}
int main(){
componentA_t *a = componentA_init();
componentB_t *b = componentB_init();
printf("%d\n", a->component.idx);
printf("%d\n", b->component.idx);
componentB_t *b2 = componentB_init();
printf("%d\n", b2->component.idx);
return 0;
}
The goal of this code is to give each component its own distinctive value based upon its type, so ideally the results of this piece of code would be that component A gets the value 0 (which it does). Component B gets the value 1 (which it does) and component B2 also gets the value 1 (it does not it gets 2)?
So if there are any pointers to this or any ideas it would be most welcome.