将 C 指针代码示例转换为 Delphi 指针语法

发布于 2024-08-30 02:46:11 字数 1062 浏览 7 评论 0原文

我正在使用二进制文件结构。读取数据的代码示例是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-09-06 02:46:11

相关代码位是

FixLeaderType *FLdrPtr; 
unsigned char RcvBuff[8192]; 

FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ]; 
  1. FldPtr 的类型为 FixLeaderType * 或指向 FixLeaderType 的指针。
  2. RcvBuff 是一个char 数组。
  3. HdrPtr->Offset[0] 解析为 ushort 值,因此 RcvBuff [ HdrPtr->Offset[0] ] 生成 char价值。
  4. & 表示不获取 char 的值,而是返回该值的地址。请注意,这意味着它是 char * 类型。
  5. char * 类型是分配给 FldPtr 的错误类型。 (FixLeaderType *) 转换类型以使其有效。这称为强制转换操作。

The bits of code that are relevant are

FixLeaderType *FLdrPtr; 
unsigned char RcvBuff[8192]; 

FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ]; 
  1. FldPtr is of type FixLeaderType *, or pointer to FixLeaderType.
  2. RcvBuff is an array of char.
  3. HdrPtr->Offset[0] resolves to an ushort value, so RcvBuff [ HdrPtr->Offset[0] ] yields a char value.
  4. The & means that instead of getting the value of the char the address of the value is returned. Note that this means that it is of type char *.
  5. The char * type is the wrong type to assign to FldPtr. The (FixLeaderType *) converts the type so that it is valid. This is called a cast operation.
硬不硬你别怂 2024-09-06 02:46:11

我认为你应该阅读以下内容:

* = pointer to

& = address of

这让事情变得容易多了

i think you should read those like:

* = pointer to

& = address of

that makes things a lot easier

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文