I have two data types with different-sized fields of the same name. I am trying to access these fields based on a parameter I pass in to a function--- say
struct smallindex
{
unsigned char index;
unsigned int count;
};
struct largeindex
{
unsigned int index;
unsigned long count;
};
This is possibly not an ideal thing to do-- but one way to implement this is, to have the function treat a pointer as a pointer of different sizes. Say, something like:
void function(int index, void *ptr)
{
array[] = {struct smallindex, struct largeindex};
array[index] *pointer = *ptr;
*pointer[1].index++;
return;
}
However, the compiler does not like this at all. I tried several other implementations-- but none of them worked. The variability in the bit field size prevented me from trying a union. Is this doable?
Alternatively, if I pad the index sizes to make sure their size matches (but their positions of fields will still not match)-- is this doable then?
The variability in the bit field size prevented me from trying a union.Uh... What? The rule is that aunionis large enough to hold its largest member, at all times, i.e. regardless of which member is currently active. There is no "variability".