I have code similar to the following:
void generateInt(int *result)
{
/* chosen by fair dice roll*/
*result = 1;
}
typedef enum colour
{
RED,
GREEN,
BLUE
} COLOUR;
int main(int, char**)
{
COLOUR col, col2;
int i;
generateInt(&i);
col = (COLOUR)i;
generateInt((int*)(&col2));
return 0;
}
After the second call to generateInt, are col and col2 both guaranteed to be equal to GREEN? I know the first version setting col is legal, but I don’t know whether it’s defined what happens if you cast pointer-to-enum to pointer-to-int, and then assign through the pointer.
That’s certainly not OK. You have no reason to believe that the underlying type of the enum is
int, and thus casting the pointers may be wildly inappropriate.Why not just change the function?