零长度数组

 1 /*
2 * =====================================================================================
3 *
4 * Filename: zeroLengthArray.cpp
5 *
6 * Description:
7 *
8 * Version: 1.0
9 * Created: 07/22/2011 01:56:35 AM
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Darius-Kylin (dyc), dongyuchi@gmail.com
14 * Company: UESTC
15 *
16 * =====================================================================================
17 */
18 #include<iostream>
19 #include<cstdlib>
20 using namespace std;
21
22 struct device
23 {
24 int num;
25 int count;
26 int reserve[0];//reserve是一个数组名;该数组没有元素;该数组的其实地址紧随结构题device之后;这种声明方法可以巧妙的实现C/C++语言里的数组扩展
27 };
28 int main()
29 {
30 struct device *p_dev=(struct device*)malloc(sizeof(struct device)+sizeof(int)*25);
31 //sizeof(int)*25是数组reserve的具体空间(25个元素)
32 p_dev->reserve[0]=99;
33 p_dev->reserve[24]=0;
34 cout<<"p_dev->reserve[0]="<<p_dev->reserve[0]<<endl;
35 cout<<"p_dev->reserve[24]="<<p_dev->reserve[24]<<endl;
36 cout<<"sizeof(struct device)="<<sizeof(struct device)<<endl;
37 // 将结构体device之后的第一个内容(int值,其实就是reserve[0]的值)赋值给变量a
38 int a=*(&(p_dev->count)+1);
39 cout<<"a="<<a<<endl;
40 return 0;
41 }

  

posted @ 2011-07-22 22:26  墨白麒麟  阅读(256)  评论(0编辑  收藏  举报