Click to See Complete Forum and Search --> : Portable C


Strogian
09-20-2001, 03:44 PM
Could someone give me an example of using typedefs or something to make a C programs (datatypes) portable? Right now, I'm writing a server, and for anything that I'd need to send through the Internet, I just use bit masks to put some part of an integer into, say, 2 characters, and send those two characters. (And then I'd just put them into an Integer on the other end). I was wondering if there was a better way of doing this.

Zoma
09-21-2001, 02:57 PM
Sounds like what you're doing is right. I basically use a buffer of some number of chars, and read everything into that. Then I figure out what I'm supposed to get. If I'm expecting a structure, I can just memcpy the data in the buffer to an instance of the structure.

To make the code portable, maybe just define a bunch of your own types in a header. Then, only the header would have to be modified for types to be correct. For example, u8, u16, u32 could be your unsigned types, and s8, s16, and s32 could be your signed types. Then just do something like "typedef unsigned short u16."