http://blog.csdn.net/zhsxcn/archive/2008/02/27/2125211.aspx
在阅读GNU/Linux内核代码时,我们会遇到一种特殊的结构初始化方式。该方式是某些C教材(如谭二版、K&R二版)中没有介绍过的。这种方式称为指定初始化(designated initializer)。下面我们看一个例子,Linux-2.6.x/drivers/usb/storage/usb.c中有这样一个结构体初始化项目:
static struct usb_driver usb_storage_driver = { .owner = THIS_MODULE, .name = "usb-storage", .probe = storage_probe, .disconnect = storage_disconnect, .id_table = storage_usb_ids, };
struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; };C99支持结构的指定初始化项目,其语法与数组的指定初始化项目近似。只是,结构的指定初始化项目使用点运算符和成员名(而不是方括号和索引值)来标识具体的元素。例如,只初始化book结构的成员value,可以这样做:
struct book surprise = { .value = 10.99 };
struct book gift = { .value = 25.99, .author = "James Broadfool", .title = "Rue for the Toad"};
struct book gift = { .value = 18.90, .author = "Philionna pestle", 0.25};
int a[6] = { [4] = 29, [2] = 15 };
int a[6] = { 0, 0, 15, 0, 29, 0 };
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
struct point { int x, y; };
struct point p = { .y = yvalue, .x = xvalue };
struct point p = { xvalue, yvalue };
struct point p = { y: yvalue, x: xvalue };
union foo { int i; double d; }; union foo f = { .d = 4 };
int a[6] = { [1] = v1, v2, [4] = v4 };
int a[6] = { 0, v1, v2, 0, v4, 0 };
int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };