// C99的指定初始化
#import "/template.typ": *
#doc-template(
title: "C99的指定初始化 ",
date: "2023年1月7日",
body: [
C99中提供了一种新的初始化方式:指定初始化(designated initialization)。这种初始化方式是少数几个C和C++不兼容的地方之一,学校里也很少会教。但是这个语法糖实在是很有用,可以少打很多字。
= 结构体
```
struct S {
int x;
int y;
};
void foo() {
struct S obj1 = {.x = 1, .y = 2};
// obj1 = {1, 2}
struct S obj2 = {.y = 2};
// obj2 = {0, 2}
}
```
= 数组
```
void bar() {
int a[5] = {[2] = 2, [4] = 4};
// a = {0, 0
...
Email: i (at) mistivia (dot) com