In C, sizeof(char) is one byte. Since htons() returns a short (the port number in network order), wouldn't you get an overflow and lose data when you assign it to filler[2]?
Not with any correct C compiler, it wont. Here's a simple test program I ran on Linux:
#include <stdio.h>
int main()
{
char filler[16] = {0};
int i;
unsigned short s1 = 0xFFFF;
unsigned long l1 = 0xFFFFFFFF;
filler[0] = (unsigned short)s1;
filler[2] = (unsigned short)s1;
filler[4] = (unsigned long)l1;
for (i = 0; i < 8; i++)
printf("filler[%d] = %d\n", i, filler[i]);
}
And when you run it, all the elements of filler that haven't been explicitly assigned to are still 0, even though the unsigned shorts and longs that were assigned to them have '1' in all their bits:
3 comments
[ 3.4 ms ] story [ 22.5 ms ] thread(And similarly for filler[4].)
Not with any correct C compiler, it wont. Here's a simple test program I ran on Linux:
And when you run it, all the elements of filler that haven't been explicitly assigned to are still 0, even though the unsigned shorts and longs that were assigned to them have '1' in all their bits: Since htons() swaps the bytes in its result, you should just be able to assign the value it returns directly: (See, for example: https://en.wikibooks.org/wiki/C_Programming/Networking_in_UN...)