被老大训了一顿,非要我省内存,4字节也要节省,本来采用struct结构,只能采用union了,
背景:
typedef enum coltype { TYPE_INT = 0, TYPE_DOUBLE, TYPE_STRING }Coltype; union To { Coltype type; int max_length; char ww[4]; }test; test.ww[0] = TYPE_DOUBLE; test.ww[1] = 0; test.ww[2] = 32; test.ww[3] = 0; int type = test.ww[0]; int len = test.ww[2];
目的:采用前两个字节作为类型说明符,后两个字节作为长度说明符,如果是字符串的话就,后面两个字节就有值,如果不是字符串的话,后面两个字节就没有值。
举例,起始地址为
4000 0x00 ww[0]
4001 0x00 ww[1]
4002 0x00 ww[2]
4003 0x00 ww[3]
可以结合大端小端结构来学习。http://blog.csdn.net/mafuli007/article/details/7341855
老大提的结构,记录一下
typedef struct col_info { double precision; char name[10]; char unit[10]; union xxx { Coltype type; struct { short type; short max_leng; }a; }type_len; }Col;