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
[758 byte] By [help_m_with_extern_linkage] at [2007-12-16]
# 1

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++

AymanShoukry at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
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);

JonathanCavesMSFT at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3
Hi Ayman,
I am sorry, this does not work either. Now it says
packet-bst.c(233) : error C2275: 'BstRequestHdr' : illegal use of this type as an expression
C:\ethereal-0.10.9\plugins\bst\bst4.hxx(180) : see declaration of 'BstRequestHdr'
packet-bst.c(233) : error C2065: 'rqstHdr' : undeclared identifier
Any other ideas?! :-)
Thanks,
Sahil

# 4
Hey Jonathan,

I tried botht the ways that you just told, but none of them work.
it gives an error:
packet-bst.c(233) : error C2275: 'BstRequestHdr' : illegal use of this type as an expression
C:\ethereal-0.10.9\plugins\bst\bst4.hxx(181) : see declaration of 'BstRequestHdr'
packet-bst.c(233) : error C2065: 'rqstHdr' : undeclared identifier

:-) Any other way to go around this?
Thanks,
Sahil

# 5
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++

AymanShoukry at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 6
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