Skip to content

Instantly share code, notes, and snippets.

@mikhailramalho
Created January 20, 2016 19:07
Show Gist options
  • Save mikhailramalho/42f992c4037e4e8babde to your computer and use it in GitHub Desktop.
Save mikhailramalho/42f992c4037e4e8babde to your computer and use it in GitHub Desktop.
typedef struct {
_Bool is_float;
union {
float f;
double s;
};
} mychoice_t;
double as_float(mychoice_t* ch)
{
if (ch->is_float) return ch->f;
else return ch->s;
}
int main()
{
mychoice_t t = {1, 2.0f};
mychoice_t t1 = {0, 2.0f};
double d = as_float(&t);
double d1 = as_float(&t1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment