//
#include "stdafx.h"
#include <stddef.h>
typedef int INT;
typedef unsigned char BYTE;
typedef short SHORT;
struct CubeInfo{
SHORT Volume;
INT Weight;
};
struct Cube{
BYTE no;
INT Len;
INT Width;
BYTE Height;
struct CubeInfo Info;
};
void ReadMember(BYTE *Data, struct Cube *c)
{
c->no = *(Data + offsetof(Cube, no));
c->Len = *((INT*)(Data + offsetof(Cube, Len)));
c->Width = *((INT*)(Data + offsetof(Cube, Width)));
c->Height = *((BYTE*)(Data + offsetof(Cube, Height)));
c->Info.Volume = *(SHORT*)(Data + offsetof(Cube, Info) + offsetof(CubeInfo, Volume));
c->Info.Weight = *(INT*)(Data + offsetof(Cube, Info) + offsetof(CubeInfo, Weight));
}
int _tmain(int argc, _TCHAR* argv[])
{
struct Cube cData = {1, 20, 30, 40, {50, 60}};
struct Cube c;
ReadMember((BYTE*)&cData, &c);
printf("c.no: %d\n", c.no);
printf("c.Len: %d\n", c.Len);
printf("c.Width: %d\n", c.Width);
printf("c.Height: %d\n", c.Height);
printf("c.Info.Volume: %d\n", c.Info.Volume);
printf("c.Info.Weight: %d\n", c.Info.Weight);
getchar();
return 0;
}
#include "stdafx.h"
#include <stddef.h>
typedef int INT;
typedef unsigned char BYTE;
typedef short SHORT;
struct CubeInfo{
SHORT Volume;
INT Weight;
};
struct Cube{
BYTE no;
INT Len;
INT Width;
BYTE Height;
struct CubeInfo Info;
};
void ReadMember(BYTE *Data, struct Cube *c)
{
c->no = *(Data + offsetof(Cube, no));
c->Len = *((INT*)(Data + offsetof(Cube, Len)));
c->Width = *((INT*)(Data + offsetof(Cube, Width)));
c->Height = *((BYTE*)(Data + offsetof(Cube, Height)));
c->Info.Volume = *(SHORT*)(Data + offsetof(Cube, Info) + offsetof(CubeInfo, Volume));
c->Info.Weight = *(INT*)(Data + offsetof(Cube, Info) + offsetof(CubeInfo, Weight));
}
int _tmain(int argc, _TCHAR* argv[])
{
struct Cube cData = {1, 20, 30, 40, {50, 60}};
struct Cube c;
ReadMember((BYTE*)&cData, &c);
printf("c.no: %d\n", c.no);
printf("c.Len: %d\n", c.Len);
printf("c.Width: %d\n", c.Width);
printf("c.Height: %d\n", c.Height);
printf("c.Info.Volume: %d\n", c.Info.Volume);
printf("c.Info.Weight: %d\n", c.Info.Weight);
getchar();
return 0;
}
本文详细介绍了如何使用C++结构体和指针进行数据读取,并通过实例展示了如何实现结构体成员的读取和打印。
1808

被折叠的 条评论
为什么被折叠?



