The problem I'm facing in C is that I'd like to have a series of structs that have a base member from another struct. e.g.
struct foo {
int a;
void (*calculate)(struct foo *);
};
struct bar {
int a;
void (*calculate)(struct foo *);
double b;
};
void do_thing(struct foo *a)
{
a->calculate(a);
}
The problem I'm facing is that the following appears to violate strict aliasing rules.
void foo_calculate(struct foo *a)
{
struct bar *b = (struct bar*)a;
}
The only way I've come up with to do this is to create a union inside struct foo
that contains all the structs that inherit from it. Is there an easier way to accomplish this?