Portable C

Sharky Forums


Results 1 to 2 of 2

Thread: Portable C

  1. #1
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post Portable C

    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.

  2. #2
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    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."
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •