Date: Sun, 28 Jan 2001 18:22:05 -0700 From: Greg Skafte <skafte+freebsd-chat@trollkarl.net> To: freebsd-chat@freebsd.org Subject: Re: C Puzzle Message-ID: <20010128182205.A98336@trollkarl.skafte.org> In-Reply-To: <20010129013145.A4834@crow.dom2ip.de>; from tmoestl@gmx.net on Mon, Jan 29, 2001 at 01:31:45AM %2B0100 References: <20010128161353.C98223@trollkarl.skafte.org> <20010129013145.A4834@crow.dom2ip.de>
next in thread | previous in thread | raw e-mail | index | archive | help
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010128182205.A98336>