I want to cast a pointer to one of two structures depending on the value of a variable. I am trying this but it is not working -
struct typeA
{
int a;
int x;
}
struct typeB
{
int b;
int a;
int x;
int z;
}
int band=1;
void *ptr;
if(band == 1)
ptr = (struct typeA *)malloc(sizeof(struct typeA));
else if(band == 2)
ptr = (struct typeB *)malloc(sizeof(struct typeB));
printf("A:%d",ptr->a);//error: structure type required instead of void
What would be the best way to do this?
EDIT: Added structure definitions and error (in code comments) to make more sense.