   Casting struct to unsigned char *
  Lets say I have the following struct:struct X{int Y;int Z;};X Object;How would I cast this to an unsigend char *, for use in networking? something kind of like this:unsigned char *Converted = (unsigned char*)Object;   
  Since you're using C++, that would go likeunsigned char* Converted = reinterpret_cast<unsigned_char*>( &Object );The "ugly" cast makes it obvious that you are using this unsafe cast for good reason, and is less likely to be overlooked than plain C-style casts.   ;  You'd do it pretty much exactly as you said only Object would have to be a pointer to an X struct or you'd have to take the address of Object using &Object.However if you're using C++ there's no guarantee to the layout of the structure in memory (actually I may be wrong here, I think it is if your structure/class contains only member variables and no functions, including constructors/destructors), hence it's generally a better idea to serialize the individual fields of the structure one at a time into the network stream (i.e. you'd write every field into an unsigned char buffer that you'd then send over the network).  The structure layout may be guarateeded in C but I'm unsure.   ;Quote:Original post by MonderThe structure layout may be guarateeded in C but I'm unsure.The structure's elements will be in declaration order, but they might be padded.So it looks like you'll have to do it like Monder said or fiddle with #pragma pack( 1 ) or the like.   ;  This is question 15 in the networking forum FAQ.   ;  Thanks eveybody for the help. My problem is now solved, and I apologise for asking an unnecessary question, I neglected to check the networking forum FAQ. Oh Well :).   