From owner-freebsd-chat Sun Jan 28 17:22:26 2001 Delivered-To: freebsd-chat@freebsd.org Received: from mage.trollkarl.net (fw.trollkarl.net [207.167.5.66]) by hub.freebsd.org (Postfix) with ESMTP id CCA3737B402 for ; Sun, 28 Jan 2001 17:22:06 -0800 (PST) Received: from trollkarl.skafte.org (root@trollkarl [192.168.100.16]) by mage.trollkarl.net (8.11.1/8.11.1) with ESMTP id f0T1M6H39779 for ; Sun, 28 Jan 2001 18:22:06 -0700 (MST) (envelope-from skafte@trollkarl.net) Received: (from skafte@localhost) by trollkarl.skafte.org (8.11.1/8.11.1) id f0T1M6H98437 for freebsd-chat@freebsd.org; Sun, 28 Jan 2001 18:22:06 -0700 (MST) (envelope-from skafte) Date: Sun, 28 Jan 2001 18:22:05 -0700 From: Greg Skafte To: freebsd-chat@freebsd.org Subject: Re: C Puzzle Message-ID: <20010128182205.A98336@trollkarl.skafte.org> References: <20010128161353.C98223@trollkarl.skafte.org> <20010129013145.A4834@crow.dom2ip.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20010129013145.A4834@crow.dom2ip.de>; from tmoestl@gmx.net on Mon, Jan 29, 2001 at 01:31:45AM +0100 Organization: Gregs Hidey Hole Disposition-Notification-To: skafte@trollkarl.net Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org I know its ugly ... its an example of what not to do .... but I forgot to check the precedence of ?: .... I also knew that they were syntactically incorrect. thanks G Quoting Thomas Moestl (tmoestl@gmx.net) On Subject: Re: C Puzzle Date: Mon, Jan 29, 2001 at 01:31:45AM +0100 > 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 -- Email: skafte@trollkarl.net ICQ: 93234105 #575 Sun Life Place * 10123 99 Street * Edmonton, AB * Canada * T5J 3H1 -- -- When things can't get any worse, they simplify themselves by getting a whole lot worse then complicated. A complete and utter disaster is the simplest thing in the world; it's preventing one that's complex. (Janet Morris) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message