C array 初始化

從在臺大唸研究所的同學那兒看來的,非常神奇的對 C 陣列作初始化的方法。

int main()
{
int x[10] =
{
[1]=2, [9]=7, [6]=4, [3]=1, [0]=5,
[2]=8, [4]=9, [5]=6, [7]=0, [8]=3,
};

int i;
for (i = 0; i < 10; i++)
printf("x[%d]=%d\n", i, x[i]);

return 0;
}

結果:
x[0]=5
x[1]=2
x[2]=8
x[3]=1
x[4]=9
x[5]=6
x[6]=4
x[7]=0
x[8]=3
x[9]=7

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*


one × = 2

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>