Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:02
Show Gist options
  • Save st98/5f08165a1622be56a2ce to your computer and use it in GitHub Desktop.
Save st98/5f08165a1622be56a2ce to your computer and use it in GitHub Desktop.
指示付き初期化子 (Designated Initializer) を使ったりして配列とか構造体を初期化してみた。 参照: http://d.hatena.ne.jp/iww/20090424/struct
#include <stdio.h>
int main(void) {
int a[5] = {
1, 0, 5
};
int b[5] = {
[0] = 1,
[2] = 5
};
printf("a: %d, %d, %d\n", a[0], a[1], a[2]);
printf("b: %d, %d, %d\n", b[0], b[1], b[2]);
return 0;
}
#include <stdio.h>
typedef struct _hoge {
char* name;
int value;
} hoge;
int main(void) {
hoge a = {
"a", 1
};
hoge b = {
.name = "c",
.value = 3
};
// GCC の古い拡張、使わない方がいいと思います…
// 参照: https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
hoge c = {
name: "b",
value: 2
};
printf("name: %s, value: %d\n", a.name, a.value);
printf("name: %s, value: %d\n", b.name, b.value);
printf("name: %s, value: %d\n", c.name, c.value);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment