From owner-freebsd-chat Sun Jan 28 16:39: 7 2001 Delivered-To: freebsd-chat@freebsd.org Received: from mail.gmx.net (pop.gmx.net [194.221.183.20]) by hub.freebsd.org (Postfix) with SMTP id 6BA1037B6A2 for ; Sun, 28 Jan 2001 16:38:49 -0800 (PST) Received: (qmail 14945 invoked by uid 0); 29 Jan 2001 00:38:16 -0000 Received: from p3e9bc01d.dip.t-dialin.net (HELO forge.local) (62.155.192.29) by mail.gmx.net (mp012-rz3) with SMTP; 29 Jan 2001 00:38:16 -0000 Received: from thomas by forge.local with local (Exim 3.16 #1 (Debian)) id 14N2ET-0001Jn-00 for ; Mon, 29 Jan 2001 01:31:45 +0100 Date: Mon, 29 Jan 2001 01:31:45 +0100 To: freebsd-chat@freebsd.org Subject: Re: C Puzzle Message-ID: <20010129013145.A4834@crow.dom2ip.de> Mail-Followup-To: tmoestl@gmx.net, freebsd-chat@freebsd.org References: <20010128161353.C98223@trollkarl.skafte.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20010128161353.C98223@trollkarl.skafte.org>; from skafte+freebsd-chat@trollkarl.net on Sun, Jan 28, 2001 at 04:13:53PM -0700 From: Thomas Moestl Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org On Sun, Jan 28, 2001 at 04:13:53PM -0700, Greg Skafte wrote: > doesn't work : > > char* fn_mtrim __P((char*)); > char* fn_mtrim (s) > char* s ;{ > short i=0; > char *buffer= (char *) calloc (256,sizeof(char)); > while (*s){ > ( *s == ' ') ? s*++ : buffer[i++] = *s++ ; > }; > return buffer; > } > > works : > > char* fn_mtrim __P((char*)); > char* fn_mtrim (s) > char* s ;{ > short i=0; > char *buffer= (char *) calloc (256,sizeof(char)); > while (*s){ > ( *s != ' ') ? buffer[i++] = s*++ : *s++ ; > }; > return buffer; > } > > Why does the first one not work and the second one work .... Precedence tells us: ( *s == ' ') ? *s++ : buffer[i++] = *s++ ; is equivalent to (( *s == ' ') ? *s++ : buffer[i++]) = *s++ ; (notice the outer brackets). I guess you mean *s++ when you write s*++. If you try this with string literal as argument (fn_mtrim("foo bar")), the string will get written into, and this may even segfault using gcc. Otherwise, the result will be wrong anyway, because s gets incremented before it is tested against ' ' (this is not really well-defined), and then incremented again when *s == ' ' and the value is assigned to *s++. - thomas P.S.: this is really ugly ;-) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message