Skip to content

Instantly share code, notes, and snippets.

@g-berthiaume
Last active December 24, 2019 00:38
Show Gist options
  • Save g-berthiaume/c02c9bcfa776ba84ca4293895529b5a8 to your computer and use it in GitHub Desktop.
Save g-berthiaume/c02c9bcfa776ba84ca4293895529b5a8 to your computer and use it in GitHub Desktop.
[C] A macro to reinterpret your own data safely

A macro to reinterpret your own data safely

#define UNION_CAST(x, destType) \
   (((union {__typeof__(x) a; destType b;})x).b)

You can then use

int myInt = UNION_CAST(myFloat, int);

Credit: cocoawithlove

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment