文献【1】中列表了几个C语言技巧。如
1、通过静态分配存储空间来初始化特定索引值的指针数组。
View Code
/* 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" /* ... */ };
2、初始化结构体中特定的元素。
(C++语言通过使用构造函数实现)
View Code
struct point { int x; int y; int z; }; struct point p = {.x = 3, .y = 4, .z = 5};
3、宏列表(不错,值得学习)。
4、编译时断言等。
参考
【1】 http://news.cnblogs.com/n/136894/