Created
December 22, 2013 18:07
-
-
Save hatsusato/8086280 to your computer and use it in GitHub Desktop.
snipet of union in C for KMC Advent Calendar 2013
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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