我将以下字节数组转换为以下结构:
** 我知道这里的 "0x80",
等格式不正确,但在我的代码中是这样。
unsigned char ReadBuffer[512] = { 80 00 00 00 50 00 00 00 01 00 40 00 00 00 01 00 00 00 00 00 00 00 00 00 FF F2 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 30 0F 00 00 00 00 00 00 30 0F 00 00 00 00 00 00 30 0F 00 00 00 00 33 20 C8 00 00 00 0C 42 E0 2A 0F 9F B9 00 00 FF}
typedef struct MFT_ATTRIBUTE {
DWORD dwType;
DWORD dwFullLength;
BYTE uchNonResFlag;
BYTE uchNameLength;
WORD wNameOffset;
WORD wFlags;
WORD wID;
LONG n64StartVCN;
LONG n64EndVCN;
WORD wDatarunOffset;
WORD wCompressionSize;
BYTE uchPadding[4];
LONGLONG n64AllocSize;
LONGLONG n64RealSize;
LONGLONG n64StreamSize;
} MFT_ATTRIBUTE, *P_MFT_ATTRIBUTE;
MFT_ATTRIBUTE* mft_attribute = (MFT_ATTRIBUTE*)ReadBuffer[0];
当我尝试打印成员时,出于某种原因我得到了某种增量值:
printf("%x ",&mft_attribute->dwType);
printf("%x ",&mft_attribute->dwFullLength);
printf("%x ",&mft_attribute->uchNonResFlag);
printf("%x ",&mft_attribute->uchNameLength);
Output:
0x80 0x84 0x88 0x89
谁能帮我澄清一下?
最佳答案
您正在打印地址,而不是值。这就是输出以这种方式增加的原因:
- 0x80 - 基地址,与第一个成员相同
dwType
- 0x84 - 第二个成员,dwFullLength,sizeof (dwType) 从开始
- 0x88 - 第三个成员,uchNonResFlag,同样是偏移量 4,sizeof(dwFullLength)
- 0x89 - 第4个成员,偏移量为1,即sizeof(uchNonResFlag)
删除输出代码中 mft_attribute 之前的 &:
printf("%x ", mft_attribute->dwType);
printf("%x ", mft_attribute->dwFullLength);
printf("%x ", mft_attribute->uchNonResFlag);
printf("%x ", mft_attribute->uchNameLength);
关于c - 字节数组到结构 - 如何访问结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33104942/