Skip to content

Instantly share code, notes, and snippets.

@hatsusato
Created December 22, 2013 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsusato/8086280 to your computer and use it in GitHub Desktop.
Save hatsusato/8086280 to your computer and use it in GitHub Desktop.
snipet of union in C for KMC Advent Calendar 2013
struct Si {
char tag;
int i;
};
struct Sp {
char tag;
int* p;
};
union U {
int i;
unsigned char c : 3; // 3bit分の情報を保持する
struct Si si;
struct Sp sp;
};
union U u = { 0 }; // 初期化は先頭メンバに対して行われる
// この時点では u.i のみが有効
struct Si si = { 'i', 42 };
u.si = si;
// この時点では u.si, u.sp.tag が有効
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment