Message-ID: <3BB5D732.AD6A64DA@yahoo.com> From: CBFalconer Organization: Ched Research X-Mailer: Mozilla 4.75 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Please add strrev proposal References: <3BB50884 DOT 347A4384 AT yahoo DOT com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 110 Date: Sat, 29 Sep 2001 15:49:58 GMT NNTP-Posting-Host: 12.90.168.45 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc06-news.ops.worldnet.att.net 1001778598 12.90.168.45 (Sat, 29 Sep 2001 15:49:58 GMT) NNTP-Posting-Date: Sat, 29 Sep 2001 15:49:58 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Radical, NetSurfer wrote: > > On Sat, 29 Sep 2001 00:28:28 GMT, CBFalconer > wrote: > > >"A. Sinan Unur" wrote: > >> > >> Radical NetSurfer wrote in > >> news:lcq8rtk1nqua2hc6rqfhmqisbd587n8t2n AT 4ax DOT com: > >> > >> > I would like to encourage everyone who has a need for > >> > strrev to come forward and encourage the maintainers of > >> > LIBC used with GCC to kindly add strrev. > >> > >> Count me against this if for no other reason that the fact that I do not > >> like extra nonstandard function which solve tiny problems. If you need the > >> functionality, you can write one for your own situation. If it is going to > >> be added to a library, the solution needs to be useful to more than just > >> one person in a particular situation. > >> > >> Anyway, the main point of my post, however, is to point out just one of > >> the gotchas with these kinds of functions. > >> > >> You give the following usage example: > >> > >> > Example > >> > printf("The reverse of %s is %s\n", str, strrev(str) ); > >> > >> Hmmmmm ..... let us see using the code you suggested: > >> > >> /* +++Date last modified: 05-Jul-1997 */ > >> /* > >> ** STRREV.C - reverse a string in place > >> ** > >> ** public domain by Bob Stout > >> */ > >> > >> #include > >> #include > >> #include > >> > >> char *strrev(char *str) { > >> char *p1, *p2; > >> > >> if (! str || ! *str) return str; > >> for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) > >> { > >> *p1 ^= *p2; > >> *p2 ^= *p1; > >> *p1 ^= *p2; > >> } > >> return str; > >> } > >> > >> int main(void) > >> { > >> char s[] = "This is a test."; > >> printf("Original String: %s\nReversed String:%s\n", s, strrev(s)); > >> return 0; > >> } > >> > >> C:\var>gcc djstrrev.c -o djstrrev.exe -O2 -Wall > >> > >> C:\var>djstrrev > >> Original String: .tset a si sihT > >> Reversed String: .tset a si sihT > > If your compiler actually outputs both strings as reversed, > THEN I am very scared your compiler is quite BROKEN. > > printf("format specifier1, format_specififer2", var1, func1); > > does: > display __current__ value of var1 FIRST using format_specifier1, > __THEN__ > display __output__ of funct1 using format_specifier2, > > If DJGPP is not doing even this trivial behavior correctly, > Borland programs would never port correctly, > neither would anything else for that matter. > > Left to right, in the order AND STATE ENCOUNTERED! > > ....however... __AFTER__ the call to printf(), __THEN__ > 'str' will contain the result of the function call; > but __NEVER__ from __WITHIN__ the printf as > you implied. Your post is VERY MISLEADING. You are wrong. The order of evaluation of parameters is NOT specified, and even if it were, the code gives the wrong answer. The first %s loads a pointer to the string. Not the string. The second function call returns a pointer to the string, and reverses it. Both pointers are the same, but since all the parameters have now been evaluated, the string proper is reversed. We can now actually call printf. So printf prints both reversed. The cause is the misleading return value from strrev. And please DO NOT send me direct e-mail when the newsgroup can handle it all. Especially do not fail to mark any such "POSTED AND MAILED", or the equivalent, so I can decide whether or not to answer directly. -- Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT XXXXworldnet DOT att DOT net) (Remove "XXXX" from reply address. yahoo works unmodified) mailto:uce AT ftc DOT gov (for spambots to harvest)