指派初始化器

版本新特性 / C99

struct Window {
    int x;
    int y;
    int width;
    int height;
};

// *** 以前 ***
/* C89:值按字段顺序依次对应,
   因此哪个数是哪个字段要靠查找,而且任何字段都不能跳过 */
struct Window bigOld = {0, 0, 800, 600};
int limitsOld[5] = {0, 0, 10, 0, 20};
/* bigOld.width is 800 */

printf("bigOld.width is %d\n", bigOld.width);

// *** 在 C99 版本中 ***
//每个值都写明自己的字段,省略的字段为零
struct Window big = {.width = 800, .height = 600};
struct Window tall = {.height = 900, .width = 300};  //在 C 中顺序任意
int limits[5] = {[2] = 10, [4] = 20};                //数组按索引
//big.width is 800
//big.x is 0
//tall.height is 900
//limits[4] is 20

printf("big.width is %d\n", big.width);
printf("big.x is %d\n", big.x);
printf("tall.height is %d\n", tall.height);
printf("limits[4] is %d\n", limits[4]);