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?
a source to share
The bits of code that matter
FixLeaderType *FLdrPtr;
unsigned char RcvBuff[8192];
FLdrPtr = (FixLeaderType *)&RcvBuff [ HdrPtr->Offset[0] ];
-
FldPtr
is of typeFixLeaderType *
or pointer toFixLeaderType
. -
RcvBuff
is an arraychar
. -
HdrPtr->Offset[0]
resolves to ushort value, thereforeRcvBuff [ HdrPtr->Offset[0] ]
gives valuechar
. -
&
means that instead of getting the valuechar
, the address of the value is returned. Note that this means it is of typechar *
. - The type
char *
is the wrong type to assignFldPtr
.(FixLeaderType *)
converts the type to be valid. This is called a cast operation.
a source to share