int data[0];
printf("sizeof data[0] is %d/n",sizeof(data));
结果:sizeof data[0] is 0
通过上面的例子可以得出一个结论 这么定义一个数组其意义就是定义一个别名
在linux的经典应用:skb 中的data
举例:
//下面的结构体定义了两种TLV
#pragma pack(1)
struct STUTimeStamp
{
unsigned char csTimeStamp[4];
};
struct STUUid
{
unsigned int uiUIN;
};
struct STUKLV : public STUUid
{
//unsigned int uiUIN;
unsigned int uiQQNum;
unsigned char ucNum;
unsigned char data[0];//value 变长的 这就是关键所在 非常灵活
};
#pragma pack(0)
#include
#include "CIndexFileMerge.h"
using namespace std;
unsigned char ucs[]=
{ 1, 0, 0, 0, 1, 0, 0, 0, 1, 120, 0, 1, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 121, 0,
0, 0, 122, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 123, 0, 0, 0, 124, 0, 0,
0, 125, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 1, 126, 0, 0, 0 };
int main()
{
int *data;
struct STUKLV *pSTUKLV = (struct STUKLV *)ucs;
while((unsigned long)pSTUKLV < (unsigned long)(ucs+sizeof(ucs)))
{
struct STUTimeStamp *pSTUTimeStamp = (struct STUTimeStamp *)pSTUKLV->data;
printf("%d/n", pSTUKLV->uiQQNum);
printf("%d/n", pSTUKLV->ucNum);
for (int i = 0; i
{
data = (int *)pSTUTimeStamp->csTimeStamp;
printf("%d/n", *data);
pSTUTimeStamp++;
}
pSTUKLV = (struct STUKLV *)((char *)pSTUKLV+sizeof(struct STUKLV)
+ pSTUKLV->ucNum*sizeof(struct STUTimeStamp));
}
// cout<
}
上面这段代码用来实现TLV(type,length,values)组 遍历待处理对象