Skip to content

Instantly share code, notes, and snippets.

@henkman
Last active May 4, 2018 16:17
Show Gist options
  • Select an option

  • Save henkman/8d42aaddf626fb6a93ba07ae820c6bed to your computer and use it in GitHub Desktop.

Select an option

Save henkman/8d42aaddf626fb6a93ba07ae820c6bed to your computer and use it in GitHub Desktop.
C struct embedding
// 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