Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 21 Aug 1998 19:05:17 +0000
From:      Mike Smith <mike@smith.net.au>
To:        Phil Gilley <pgilley@metronet.com>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: Stupidity or compiler bug? 
Message-ID:  <199808211905.TAA11658@dingo.cdrom.com>
In-Reply-To: Your message of "Fri, 21 Aug 1998 20:50:37 EST." <Pine.HPP.3.95.980821204532.12165B-100000@fohnix.metronet.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> Am I doing something really stupid here or is this a compiler bug?
> The output of this program under 2.2.7-RELEASE is "1.2.3.4 1.2.3.4"
> which isn't what I was expecting.  "1.2.3.4 5.6.7.8" is what I was
> hoping for.
> 
> #include <stdio.h>
> #include <netinet/in.h>
> #include <netinet/in_systm.h>
> #include <netinet/ip.h>
> #include <arpa/inet.h>
> 
> void main() {
> 	struct ip iph;
> 
> 	iph.ip_src.s_addr = ntohl(0x01020304);
> 	iph.ip_dst.s_addr = ntohl(0x05060708);
> 
> 	printf("%s %s\n", inet_ntoa(iph.ip_src), inet_ntoa(iph.ip_dst));
> }

inet_ntoa returns a pointer to a static buffer.  gcc is evaluating 
arguments right to left (ie. in stacking order), so the ip_src call is 
made last, but the pointer is the same in both cases.  Try:

	printf("%s ", inet_ntoa(iph.ip_src));
	printf("%s\n", inet_ntoa(iph.ip_dst));

or use strdup() if your real intent is to pass the strings around.

-- 
\\  Sometimes you're ahead,       \\  Mike Smith
\\  sometimes you're behind.      \\  mike@smith.net.au
\\  The race is long, and in the  \\  msmith@freebsd.org
\\  end it's only with yourself.  \\  msmith@cdrom.com



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199808211905.TAA11658>