data[0] 的妙用

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 <iostream>
#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<pSTUKLV->ucNum; i++)
        {
            data = (int *)pSTUTimeStamp->csTimeStamp;
            printf("%d/n", *data);
            pSTUTimeStamp++;
        }
        pSTUKLV = (struct STUKLV *)((char *)pSTUKLV+sizeof(struct STUKLV)
                + pSTUKLV->ucNum*sizeof(struct STUTimeStamp));
    }

    //    cout<<sizeof (class CSex);
    //    cout<<sizeof (struct STUKLV);
    return 0;
}
上面这段代码用来实现TLV(type,length,values)组  遍历待处理对象

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值