人生如逆旅,我亦是行人!江湖人称wsjjeremy.blog.chinaunix.net
ubuntuer
全部博文(930)
intern(3)
string(19)
正则表达式(5)
2011年(60)
2010年(220)
2009年(371)
2008年(279)
baocheng
nba76ers
renjian2
qq576709
mcn304
zibuyule
西农魔峰
曾德标
zhuqing_
shanck
tendy
moshangx
wb123456
smile124
hjshajsh
chenhong
bzhao
python16
分类: LINUX
2009-02-06 14:03:16
可以使用info gcc "c ext" zero指令查看 5.11 Arrays of Length Zero ========================== Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object: struct line { int length; char contents[0]; }; struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length; In ISO C90, you would have to give `contents' a length of 1, which means either you waste space or complicate the argument to `malloc'. #include <stdlib.h> #include <stdio.h> struct device{ int count; int reserve[0]; //reserve是一个数组名;该数组没有元素;该数组的其实地址紧随结构题device之后;这种声明方法可以巧妙的实现C语言里的数组扩展 }; int main() { struct device * p_dev = (struct device *) malloc (sizeof(struct device) + sizeof(int)*25); //sizeof(int)*25是数组reserve的具体空间(25个元素) p_dev->reserve[0] = 10; p_dev->reserve[24] = 0; printf("p_dev->reserve[24] = %d\n", p_dev->reserve[24]); printf("sizeof(struct device) = %d\n",sizeof(struct device)); // 将结构体device之后的第一个内容(int值,其实就是reserve[0]的值)赋值给变量a // int a = *((&(p_dev->count)) + 1); int a = *(&p_dev->count + 1); printf("a = %d\n", a); } [root@zj cstudy]# gcc -o array0 array0.c [root@zj cstudy]# ./array0 p_dev->reserve[24] = 0 sizeof(struct device) = 4 a = 10
上一篇:cannot restore segment prot after reloc的问题
下一篇:printf函数小技巧:用变量控制输出宽度
登录 注册