Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: strcmp Message-ID: <6386@goanna.cs.rmit.oz.au> Date: 19 Jun 91 09:48:31 GMT References: <2695@m1.cs.man.ac.uk> <1991Jun18.074029.12226@panix.uucp> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 19 In article <1991Jun18.074029.12226@panix.uucp>, yanek@panix.uucp (Yanek Martinson) writes: > For strcmp(s,t): while(*s++==*t++&&*s&&*t); return *s-*t; Sorry, there's a mistake. Let p = "\0followed by 50 million bytes of junk"; Then strcmp(p,p) will set s=p, t=p, find *s == *t == '\0', increment s and t, and because the _next_ bytes aren't \0 it will keep going. Not good. In fact, if you call strcmp("\0a", "\0b") with that definition, it will claim they _aren't_ equal, but they are both empty! Keep it simple: while (*s == *t && *s != '\0' && *t != '\0') s++, t++; return *s - *t; Much as I hate to say this, a good compiler is likely to do just as well from this as from anything cleverer. -- Q: What should I know about quicksort? A: That it is *slow*. Q: When should I use it? A: When you have only 256 words of main storage.