Groovy hepcat Christopher Benson-Manica was jivin' on Mon, 2 Feb 2004
15:44:03 +0000 (UTC) in comp.lang.c.
Too obfuscated?'s a cool scene! Dig it!
(I want the purpose of the following code to be clear, and I'm sure
I've failed ;))
/* Assume that str is always of the form ([0-9]+[.])*[0-9]+ */
void DoStuff( const char *str )
{
const char *cp;
for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
/* do things with atoi(cp) */
}
}
Ick!
Perhaps
void DoStuff( const char *str )
{
const char *cp;
cp=str;
while( cp ) {
if( cp == '.' )
^
You're missing a dereference here.
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}
would be better? Any even better suggestions?
That's better, but still not great. You're effectively testing the
same value twice (strchr(cp, '.') followed by if(*cp == '.') in the
next iteration). A better approach would be to call strchr() in the
controlling expression of the loop, like so:
void DoStuff(const char *str)
{
while(NULL != (str = strchr(str, '.')))
{
/* do yer thing with str */
}
}
If you find that unclear, you might prefer to do it this way (it
amounts to the same thing):
void DoStuff(const char *str)
{
str = strchr(str, '.');
while(str)
{
/* do yer thing with str */
str = strchr(str, '.');
}
}
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?