Too obfuscated?

Discussion in 'C Programming' started by Christopher Benson-Manica, Feb 2, 2004.

  1. (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.
    Christopher Benson-Manica, Feb 2, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    nrk Guest

    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
    nrk, Feb 2, 2004
    #2
    1. Advertising

  3. 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.
    Jarno A Wuolijoki, Feb 2, 2004
    #3
  4. 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"?
    Peter Shaggy Haywood, Feb 5, 2004
    #4
  5. Christopher Benson-Manica

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Feb 5, 2004
    #5
  6. Peter "Shaggy" Haywood <> 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.
    Christopher Benson-Manica, Feb 5, 2004
    #6
  7. CBFalconer <> 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.
    Christopher Benson-Manica, Feb 5, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    675
  2. chris kramer
    Replies:
    5
    Views:
    556
    chris kramer
    Apr 9, 2004
  3. fix

    Obfuscated xsl

    fix, Nov 27, 2003, in forum: XML
    Replies:
    4
    Views:
    579
    Patrick TJ McPhee
    Nov 28, 2003
  4. Thomas Garai

    obfuscated c/c++

    Thomas Garai, Jun 22, 2004, in forum: C++
    Replies:
    5
    Views:
    472
    Prateek R Karandikar
    Jun 22, 2004
  5. G. Nick D'Andrea

    Obfuscated Languages Interpreter

    G. Nick D'Andrea, Jun 30, 2003, in forum: C Programming
    Replies:
    3
    Views:
    733
    David Rubin
    Jul 2, 2003
Loading...

Share This Page