moving array bits to another array of another type

Started by
2 comments, last by ApochPiQ 10 years, 6 months ago

I have in C++

RAWMOUSE mouses[2];

char info[48]; // size of one RAWMOUSE is 24 bytes

I want to pass the bits of info to mouse and viceversa

Advertisement

This is a really, really, really bad idea, and you should avoid it, but here's how it would work:

// Copy mouse data into info array
const char * pmouse = reinterpret_cast<const char *>(&mouses[0]);
std::copy(pmouse, pmouse + sizeof(RAWMOUSE * 2), &info[0]);
 
// Copy info array into mouse data
char * pmouse = reinterpret_cast<char *>(&mouses[0]);
std::copy(&info[0], &info[48], pmouse);

At the very least, use some static asserts to ensure that your RAWMOUSE size and the size of the info array are lined up correctly.

But yeah, you really just shouldn't do that. Depending on what you're trying to accomplish, there's basically guaranteed to be a better solution.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

what I am doing is sending the raw mouse input of 2 mouse to another computer using datagram sockets.

sometimes the send() will send incomplete data and will tell me how many bytes of the data has been sent and I have to calculate how much is left and send the rest

so I have to pass the RAWMOUSE array to a char array so when I see that there are lets say 4 bytes left to send I can send them:

char info[48];

char* pointer;

pointer=info;

send(pointer);

//then I find that 44 of the 48 bytes were sent

pointer+=44;

send(pointer);

ooHH!!I just need to use da "pointer" with "mouses" sorry now i know how to do it!!!

char* pointer;

pointer=mouses; //mouses!!!!!<---------------------------------

send(pointer);

//then I find that 44 of the 48 bytes were sent

pointer+=44;

send(pointer);

Look into serialization. This approach is going to bite you sooner or later, and you may as well do it right :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement