Last active
May 4, 2018 16:17
-
-
Save henkman/8d42aaddf626fb6a93ba07ae820c6bed to your computer and use it in GitHub Desktop.
C struct embedding
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
| // gcc -std=c99 -Wall -s -O3 -fms-extensions -Wno-microsoft-anon-tag -o test test.c | |
| // clang -std=c99 -Wall -s -O3 -fms-extensions -Wno-microsoft-anon-tag -o test test.c | |
| #include <stdio.h> | |
| typedef struct | |
| { | |
| unsigned a; | |
| } A; | |
| typedef struct | |
| { | |
| unsigned b; | |
| } B; | |
| typedef struct | |
| { | |
| union { | |
| A; | |
| A A; | |
| }; | |
| union { | |
| B; | |
| B B; | |
| }; | |
| } AB; | |
| static void A_print(A *a) { | |
| printf("a: %u\n", a->a); | |
| } | |
| static void B_print(B *b) { | |
| printf("b: %u\n", b->b); | |
| } | |
| static void AB_print(AB *ab) { | |
| A_print(&ab->A); | |
| B_print(&ab->B); | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| AB ab; | |
| ab.A.a = 5; | |
| ab.b = 10; | |
| AB_print(&ab); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment