将 C 指针代码示例转换为 Delphi 指针语法
我正在使用二进制文件结构。读取数据的代码示例是C语言的,我需要在Delphi中读取它。我赶紧补充一下,我没有 C 编程经验。
鉴于以下
typedef struct {
uchar ID, DataSource;
ushort ChecksumOffset;
uchar Spare, NDataTypes;
ushort Offset [256];
} HeaderType;
...
typedef struct {
ushort ID;
...
ushort DistanceToBin1Middle,TransmitLength;
} FixLeaderType;
...
HeaderType *HdrPtr;
FixLeaderType *FLdrPtr;
unsigned char RcvBuff[8192];
void DecodeBBensemble( void )
{
unsigned short i, *IDptr, ID;
FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];
if (FLdrPtr->NBins > 128)
FLdrPtr->NBins = 32;
...
情况,我遇到的困难是:
FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];
根据我的了解,[ HdrPtr->Offset[0] ];
将返回第一个 Offset 数组项的值HdrPtr 指向的 HeaderType 结构?那么相当于 HdrPtr^.Offset[0]
吗?
那么 &RcvBuff [ HdrPtr->Offset[0] ];
应该返回包含索引的 RcvBuff 数组项的值的内存地址,因此相当于 @RecBuff[HdrPtr^。偏移量[0]]
?
然后我迷失了 (FixLeaderType *)..
。有人可以帮助准确解释 FldrPtr 引用的内容吗?
I am working with a binary file structure. The code example for reading the data is in C, and I need to read it in Delphi. I hasten to add I have no C programming experience.
Given the following
typedef struct {
uchar ID, DataSource;
ushort ChecksumOffset;
uchar Spare, NDataTypes;
ushort Offset [256];
} HeaderType;
...
typedef struct {
ushort ID;
...
ushort DistanceToBin1Middle,TransmitLength;
} FixLeaderType;
...
HeaderType *HdrPtr;
FixLeaderType *FLdrPtr;
unsigned char RcvBuff[8192];
void DecodeBBensemble( void )
{
unsigned short i, *IDptr, ID;
FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];
if (FLdrPtr->NBins > 128)
FLdrPtr->NBins = 32;
...
The bit I am having difficulty following is this:
FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];
From the little I understand, [ HdrPtr->Offset[0] ];
would be returning the value of the first Offset array item from the HeaderType struct pointed to by HdrPtr? So equivalent to HdrPtr^.Offset[0]
?
Then &RcvBuff [ HdrPtr->Offset[0] ];
should be returning the memory address containing the value of the RcvBuff array item indexed, so equivalent to @RecBuff[HdrPtr^.Offset[0]]
?
Then I get lost with (FixLeaderType *)..
. Could someone please help explain exactly what is being referenced by FldrPtr ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相关代码位是
FldPtr
的类型为FixLeaderType *
或指向FixLeaderType
的指针。RcvBuff
是一个char
数组。HdrPtr->Offset[0]
解析为 ushort 值,因此RcvBuff [ HdrPtr->Offset[0] ]
生成char
价值。&
表示不获取char
的值,而是返回该值的地址。请注意,这意味着它是char *
类型。char *
类型是分配给FldPtr
的错误类型。(FixLeaderType *)
转换类型以使其有效。这称为强制转换操作。The bits of code that are relevant are
FldPtr
is of typeFixLeaderType *
, or pointer toFixLeaderType
.RcvBuff
is an array ofchar
.HdrPtr->Offset[0]
resolves to an ushort value, soRcvBuff [ HdrPtr->Offset[0] ]
yields achar
value.&
means that instead of getting the value of thechar
the address of the value is returned. Note that this means that it is of typechar *
.char *
type is the wrong type to assign toFldPtr
. The(FixLeaderType *)
converts the type so that it is valid. This is called a cast operation.我认为你应该阅读以下内容:
这让事情变得容易多了
i think you should read those like:
that makes things a lot easier