Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Too obfuscated?

Reply
Thread Tools

Too obfuscated?

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-02-2004
(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) */
}
}

Perhaps

void DoStuff( const char *str )
{
const char *cp;

cp=str;
while( cp ) {
if( cp == '.' )
cp++;
/* do things with atoi(cp) */
cp=strchr( cp, '.' );
}
}

would be better? Any even better suggestions?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
nrk
Guest
Posts: n/a
 
      02-02-2004
Christopher Benson-Manica wrote:

> (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) */
> }
> }
>
> Perhaps
>
> void DoStuff( const char *str )
> {
> const char *cp;
>
> cp=str;
> while( cp ) {
> if( cp == '.' )
> cp++;
> /* do things with atoi(cp) */
> cp=strchr( cp, '.' );
> }
> }
>
> would be better? Any even better suggestions?
>


How about:

void do_stuff(const char *str) {
char *cptr;

while ( *str ) {
long l = strtol(str, &cptr, 0); /* set base to suit your needs */
/* do stuff with l */
/* replace with *cptr == '.' if needed below */
str = *cptr ? cptr + 1 : cptr;
}
}

You do know that strtol is preferred over atoi, right?

-nrk.

--
Remove devnull for email
 
Reply With Quote
 
 
 
 
Jarno A Wuolijoki
Guest
Posts: n/a
 
      02-02-2004
On Mon, 2 Feb 2004, Christopher Benson-Manica wrote:

> (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) */
> }
> Any even better suggestions?


I'd keep it simple:

if (str) {
const char *cp=str;
for (; {
/* do things with atoi(cp) */
cp=strchr(cp, '.');
if (!cp) break;
cp++;
}
}

None of the codes process stuff after the last '.' yet the regexp
indicates there is some, btw.

 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      02-05-2004
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"?
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-05-2004
Peter Shaggy Haywood wrote:
>

.... snip ...
>
> void DoStuff(const char *str)
> {
> str = strchr(str, '.');
> while(str)
> {
> /* do yer thing with str */
> str = strchr(str, '.');
> }
> }


That misses the essential characteristic of starting after the
'.'. He might try:

void DoStuff(const char *str)
{
while (str = strchr(str, '.') { /* found a period */
str++; /* skip over it */
/* do yer thing with str */
}
}

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-05-2004
Peter "Shaggy" Haywood <(E-Mail Removed)> spoke thus:

>> for ( cp=str ; cp ; cp=(cp=strchr(cp,'.'))?cp+1:0 ) {
>> /* do things with atoi(cp) */
>> }


> Ick!


Mikey likes it!!

>> if( cp == '.' )

^
> You're missing a dereference here.


Thank you.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-05-2004
CBFalconer <(E-Mail Removed)> spoke thus:

> void DoStuff(const char *str)
> {
> while (str = strchr(str, '.') { /* found a period */
> str++; /* skip over it */
> /* do yer thing with str */
> }
> }


That I like. Although I like to think my original implementation had
a certain twisted charm to it.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Client Assembly.. what is too big or too small ??? Martin ASP .Net 0 08-04-2004 08:47 AM
Are these pictures too dark or/and too large? Luigi Donatello Asero HTML 13 05-21-2004 04:54 AM
Problems with imaging (too slow or too much RAM) SB Java 0 08-05-2003 11:06 AM
Re: Is this just too hard for folks here? Or too stupid? Mark Parnell HTML 0 06-23-2003 11:02 PM
Re: Is this just too hard for folks here? Or too stupid? Mike Foster HTML 0 06-23-2003 07:14 PM



Advertisments