I have a void pointer that I casted to a char*. I'm trying to put different data types in that char* without having to cast so much. This includes doubles, floats, int's, etc..
char* Data = static_cast<char*>(GivenPtr);
switch(reinterpret_cast<int>(Data)[0]) //Or should it be reinterpret_cast<int>(Data[0])
{
case OBJECT: //defined as 301;
{
Data[0] = 863; //Overflow?
Data[1] = 970;
//OR should I be doing:
int* pData = reinterpret_cast<int*>(Data);
pData[0] = 863;
pData[1] = 970;
double* pDub = reinterpret_cast<double*>(pData[2???]); //Index 2???
pDub[0] = 57.99;
}
break;
}
Write my char* to a buffer and read it back exactly the same way it was written to:
case OBJECT2:
{
int X = reinterpret_cast<int>(Data[1]); //Or reinterpret_cast(Data)[1]?
std::cout<<X;
double pDub = reinterpret_cast<double>(Data[??]); //?????
std::cout<<pDub;
}
break;
How can I tell what Indicies to write to after each cast? I really don't want to cast so much. Is there a way to just write it straight to a char* and then read straight from that char? Better yet, can I do this with a void?