This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: -Wconversion warning with htons
- From: Ian Lance Taylor <iant at google dot com>
- To: Yang Zhang <yanghatespam at gmail dot com>
- Cc: GCC-help <gcc-help at gcc dot gnu dot org>
- Date: Mon, 23 Feb 2009 12:55:16 -0800
- Subject: Re: -Wconversion warning with htons
- References: <49A1CCC8.9060205@gmail.com>
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