Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert char pointer (char*) to struct

Tags:

c++

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?

like image 966
InfinateOne Avatar asked Apr 11 '10 11:04

InfinateOne


2 Answers

If it's C++:

char* theCharPtr; // has your converted data

UserPacket* fromChar = reinterpret_cast<UserPacket*>(theCharPtr);
like image 81
Zoli Avatar answered Nov 15 '22 07:11

Zoli


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.

like image 45
Sparky Avatar answered Nov 15 '22 06:11

Sparky