Skip to content

Instantly share code, notes, and snippets.

@jwerle
Last active August 29, 2015 14:01
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 jwerle/872f13f3213fc05109e7 to your computer and use it in GitHub Desktop.
Save jwerle/872f13f3213fc05109e7 to your computer and use it in GitHub Desktop.
C struct initializations
$ cc s-struct-initalization.c && ./a.out
foo 1 bar
biz 2 baz
beep 3 boop
#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