This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: -Wconversion warning with htons


Yang Zhang <yanghatespam@gmail.com> 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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]