Converting sample C code to Delphi pointer syntax

I am working with a binary file structure. The sample code for reading data is in C and I need to read it in Delphi. I hasten to add that I have no C programming experience.

Considering 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 ran into is the following:

FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];

      

As I understand it [ HdrPtr->Offset[0] ];

will return the first element of the Offset array from the HeaderType structure pointed to by HdrPtr? So the equivalent HdrPtr^.Offset[0]

?

&RcvBuff [ HdrPtr->Offset[0] ];

Should then return a memory address containing the value of the element of the RcvBuff array indexed, which is equivalent @RecBuff[HdrPtr^.Offset[0]]

?

Then I get lost with (FixLeaderType *)..

. Can someone please help explain what exactly is being referenced by FldrPtr?

+2


a source to share


2 answers


The bits of code that matter

FixLeaderType *FLdrPtr; 
unsigned char RcvBuff[8192]; 

FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ]; 

      



  • FldPtr

    is of type FixLeaderType *

    or pointer to FixLeaderType

    .
  • RcvBuff

    is an array char

    .
  • HdrPtr->Offset[0]

    resolves to ushort value, therefore RcvBuff [ HdrPtr->Offset[0] ]

    gives value char

    .
  • &

    means that instead of getting the value char

    , the address of the value is returned. Note that this means it is of type char *

    .
  • The type char *

    is the wrong type to assign FldPtr

    . (FixLeaderType *)

    converts the type to be valid. This is called a cast operation.
+4


a source


I think you should read such as:

* = pointer to

& = address of

      



which makes things so much easier

+3


a source







All Articles