您的位置:首页 > 其它

关于不同类型的结构体的数组的读取和保存的测试程序

2010-01-09 16:45 477 查看
#include "stdio.h"
#include "string.h"

typedef struct
{
int  nData;
char chData;
}Tstruct1;

typedef struct
{
int   nData;
char  chData;
short wData;
}Tstruct2;

Tstruct1 tTestt1 = {100, '1'};
Tstruct2 tTestt2 = {200, '2', 20};

const char *Stable[]=
{
(const char *)(&tTestt1),
(const char *)(&tTestt2),
};

void GetStructT(int type, void *pvOutBuf, int dwLen)
{
char *lpstrBuf = NULL;

lpstrBuf = (char *)(Stable[type]);
memcpy(pvOutBuf, (char *)lpstrBuf, dwLen);
}

void SetStructT(int type, void *pvOutBuf, int dwLen)
{
char *lpstrBuf = NULL;

lpstrBuf = (char *)(Stable[type]);
memcpy(lpstrBuf, pvOutBuf, dwLen);
}

int main(void)
{
Tstruct1 at1 = {0};
Tstruct2 at2 = {0};

GetStructT(0, &at1, sizeof(Tstruct1));
printf("struct1: nData:%d, chData:%c/n", at1.nData, at1.chData);
GetStructT(1, &at2, sizeof(Tstruct2));
printf("struct2: nData:%d, chData:%c, wData:%d/n", at2.nData, at2.chData, at2.wData);
at2.nData  = 700;
at2.chData = '8';
at2.wData  = 66;
SetStructT(0, &at2, sizeof(at2));
printf("struct2: nData:%d, chData:%c, wData:%d/n", at2.nData, at2.chData, at2.wData);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: