InfinateOne
InfinateOne

Reputation: 81

Convert char pointer (char*) to struct

I have a struct:

//Custom packet structure.
struct UserPacket
{
 __int64 pingTime;
} CustomPacket;

I have already figured out how to convert it to a char*. Now I want to convert the char* back to the struct. Any suggestions?

Upvotes: 7

Views: 23200

Answers (2)

Zoli
Zoli

Reputation: 1137

If it's C++:

char* theCharPtr; // has your converted data

UserPacket* fromChar = reinterpret_cast<UserPacket*>(theCharPtr);

Upvotes: 12

Sparky
Sparky

Reputation: 14057

Typecast it. Here are some examples (two using type casting).

CustomPacket  customPacket;

char *          p = (char *) &customPacket;

CustomPacket *  pPacket    = (CustomPacket *) p;
CustomPacket *  pAlternate = &customPacket;

Hope this helps.

Upvotes: 5

Related Questions