Re: -Wconversion warning with htons

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yang Zhang <yanghatespam@xxxxxxxxx> writes:

> When compiling the following:
>
> #include <arpa/inet.h>
> int main() {
>   sockaddr_in sa;
>   unsigned short x = 3;
>   sa.sin_port = (unsigned short) htons((unsigned short) x);
>   return 0;
> }
>
> I get:
>
> $ g++ conversion_weirdness.cc -O3 -Wconversion
> conversion_weirdness.cc: In function ‘int main()’:
> conversion_weirdness.cc:5: warning: conversion to ‘short unsigned int’ 
>>From ‘int’ may alter its value
>
> The man page says the signature of htons is
>
>   uint16_t htons(uint16_t hostshort);
>
> I've been fiddling around with inserting casts at various places,
> declaring extra intermediate variables, using uint16_t, etc., but I
> can't seem to figure out what precisely is causing this warning.  Any
> hints?

Try using the --save-temps option and look at the preprocessed code in
conversion_weirdness.ii.  On many systems htons is implemented as a
macro.

Ian


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux