Mauaka
asked on
Typecasting char* to a structure
Situation:
I have created an application which sends a structure (composed mostly of DWORD, UWORD, etc...) from one PC to another. Take into mind that the structure of the message also uses nested structures (meaning structures within the main structure). This communication is made possible by windows socket. The communication of socket message passing (send() and recv()) uses char pointer. So, to make the message pass through the other side, I typcasted my message into char pointer.
Problem:
After being received by the recv() function, the message is in char pointer. How can I convert the char pointer back into the structure.
Actions taken:
1. Type casted inot structure directly: structure = (structure)buffer, structure = (structure)buffer;
2. Changed the message passed into plain character. The message was displayed correctly, meaning, the message passed is correctly sent.
3. Used memcpy. As expected, it failed.
4. Changed the receiving structure into a structure pointer: structure* = (structure*)buffer, structure* = (structure*)&buffer -->nice try.
5. I even tied typecasting it to void.
Your help will greatly be appreciated. Thank you.
I have created an application which sends a structure (composed mostly of DWORD, UWORD, etc...) from one PC to another. Take into mind that the structure of the message also uses nested structures (meaning structures within the main structure). This communication is made possible by windows socket. The communication of socket message passing (send() and recv()) uses char pointer. So, to make the message pass through the other side, I typcasted my message into char pointer.
Problem:
After being received by the recv() function, the message is in char pointer. How can I convert the char pointer back into the structure.
Actions taken:
1. Type casted inot structure directly: structure = (structure)buffer, structure = (structure)buffer;
2. Changed the message passed into plain character. The message was displayed correctly, meaning, the message passed is correctly sent.
3. Used memcpy. As expected, it failed.
4. Changed the receiving structure into a structure pointer: structure* = (structure*)buffer, structure* = (structure*)&buffer -->nice try.
5. I even tied typecasting it to void.
Your help will greatly be appreciated. Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hey Mauaka
I think your problems, might start, at the sending side:
if you cast a struct pointer, with pointers to other struct inside, into a char *, the result, will not contain the sub structs.
could you post the details, of the structs, and the sending code.
Yotam
I think your problems, might start, at the sending side:
if you cast a struct pointer, with pointers to other struct inside, into a char *, the result, will not contain the sub structs.
could you post the details, of the structs, and the sending code.
Yotam
yotam, do not worry. if you look at the prototype it is [code]int recv(SOCKET s, char* buf, int len, int flags);[/code]
HonorGod, your answer is syntactically correct but imagine what would happen when recv is called second time with same buffer?
recv(sock, (char*)&some_struct, len, flags) would save some time over using FillStructure().
HonorGod, your answer is syntactically correct but imagine what would happen when recv is called second time with same buffer?
recv(sock, (char*)&some_struct, len, flags) would save some time over using FillStructure().
suppose if your structure is
struct Anystruct
{
int a;
char *str;
};
If your structure contains pointer variables, then i think when you cast it to char and send it, you are actually sending the pointer address and not the value pointed to by the pointer. when you receive the data at the other end ie other pc, and cast it to structure the pointer variable is no longer valid.
This could be a problem.
please any body correct me if i am wrong.
struct Anystruct
{
int a;
char *str;
};
If your structure contains pointer variables, then i think when you cast it to char and send it, you are actually sending the pointer address and not the value pointed to by the pointer. when you receive the data at the other end ie other pc, and cast it to structure the pointer variable is no longer valid.
This could be a problem.
please any body correct me if i am wrong.
true numansiddique. I guess the programmer has to take care of that manually.
The question was posed in such a way as to be very specific. So the answer that was provided was also specific.
Any variable can be overwritten. C and C++ are generally considered mid level languages because they are not
far from the architecture of the hardware. C was originally designed to be a high-level assembly language for the PDP process to which K & R had access.
Any variable can be overwritten. C and C++ are generally considered mid level languages because they are not
far from the architecture of the hardware. C was originally designed to be a high-level assembly language for the PDP process to which K & R had access.
struct AnyStruct * anyPtr;
anyPtr = (struct AnyStruct *) buffer;