一、指定的初始化
int fibs[] = {1, 1, 2, 3, 5};
二、数组
/* Entries may not correspond to actual numbers. Some entries omitted. */
#define EINVAL 1
#define ENOMEM 2
#define EFAULT 3
/* ... */
#define E2BIG 7
#define EBUSY 8
/* ... */
#define ECHILD 12
/* ... */
现在,假设我们想为每个错误码提供一个错误描述的字符串。
为了确保数组保持了最新的定义,无论头文件做了任何修改或增补,我们都可以用这个数组指定的语法
char *err_strings[] = {
[0] = "Success",
[EINVAL] = "Invalid argument",
[ENOMEM] = "Not enough memory",
[EFAULT] = "Bad address",/* ... */
[E2BIG ] = "Argument list too long",
[EBUSY ] = "Device or resource busy",/* ... */
[ECHILD] = "No child processes"/* ... */
};
三、结构体与联合体
struct point {int x;int y;int z;}
struct point p = {
.x = 3,
.y = 4,
.z = 5
};
四、宏列表
**
#define FLAG_LIST(_) \
_(InWorklist) \
_(EmittedAtUses) \
_(LoopInvariant) \
_(Commutative) \
_(Movable) \
_(Lowered) \
_(Guard)