Conversion from C++ to C
Hi ,
I am stuck at this particular problem in my program. here is the C++ code that i want to convert to C, and there is just ONE line that i am not able to convert, the one in the function (DisplayRequest). I would truely appriciate any help offered.
/*****C++ CODE******************/
struct BstRequestHdr
{
UINT8 flags;
UINT8 version;
UINT16 count;
UINT16 code;
};
void DisplayRequest(UINT8 *pxData)
{
BstRequestHdr *rqstHdr = (BstRequestHdr*)(pxData-2); .
.
.
}
/*****************END***************/
Thanks,
Sahil
I believe in C, you might need a typedef, to illustrate, the following example would work for both C & C++:
typedef struct
{
int flags;
int version;
int count;
int code;
}BstRequestHdr;
void DisplayRequest(int *pxData)
{
BstRequestHdr *rqstHdr = (BstRequestHdr*)(pxData-2);
}
Thanks,
Ayman Shoukry
VC++
In C the names of structs live in their own namespace so they are not accessible by normal name lookup.
There are two ways to fix your code. The first, as Ayman says, is to use a typedef: though I would probably code it as follows:
struct BstRequestHdr
{
int flags;
int version;
int count;
int code;
};typedef struct BstRequestHdr BstRequestHdr;
But that is just my personal perference.
The second way is to explicitly tell the C compiler that you are looking for the name of a struct: to do this you preprend the identifier with the 'struct' keyword.
struct BstRequestHdr *rqstHdr = (struct BstRequestHdr*)(pxData-2);
Can you try putting this sample in a seperate file (say t.c) and do cl /c t.c:
typedef struct
{
int flags;
int version;
int count;
int code;
}BstRequestHdr;
void DisplayRequest(int *pxData)
{
BstRequestHdr *rqstHdr = (BstRequestHdr*)(pxData-2);
}
It compiles with no errors for me.
Thanks,
Ayman Shoukry
VC++
I just figured it out!!
In C, we can not initialize variables during run time!!!!
so, when I did
BstRequestHdr *rqstHdr;
on the top, and then did the conversion:
rqstHdr = (BstRequestHdr*)(pxData-2); it worked!
Thanks again for your time! :-)
Best Regards,
Sahil