Last active
August 29, 2015 14:01
-
-
Save jwerle/872f13f3213fc05109e7 to your computer and use it in GitHub Desktop.
C struct initializations
This file contains 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
$ cc s-struct-initalization.c && ./a.out | |
foo 1 bar | |
biz 2 baz | |
beep 3 boop |
This file contains 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
#include <stdio.h> | |
#include <string.h> | |
struct data_s { | |
int type:4; | |
char *buf; | |
void *extra; | |
}; | |
#define inspect(d) { \ | |
printf("%s %d %s\n", \ | |
d.buf, d.type, d.extra); \ | |
} | |
int | |
main (void) { | |
{ | |
struct data_s data; | |
data.type = 1; | |
data.buf = "foo"; | |
data.extra = "bar"; | |
inspect(data); | |
} | |
{ | |
struct data_s data = { 2, "biz", "baz" }; | |
inspect(data); | |
} | |
{ | |
struct data_s data; | |
memset(&data, 3, sizeof(data)); | |
data.buf = "beep"; | |
data.extra = "boop"; | |
inspect(data); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment