Problematic code in K&R

Discussion in 'C Programming' started by Gregor H., Apr 19, 2007.

  1. Gregor H.

    Gregor H. Guest

    Hello C programmers,

    in K&R (2nd edition) the authors published the following code for dealing with
    command line arguments (p. 117):

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    while (--argc > 0 && (*++argv)[0] == '-')
    while (c = *++argv[0])
    switch (c) {
    case 'x':
    except = 1;
    break;
    case 'n':
    number = 1;
    break;
    default:
    printf("find: illegal option %c\n", c);
    argc = 0;
    found = -1;
    break;
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    In addition they give the following explanation of this part of the program:

    "argc is decremented and argv is incremented before each optional argument.
    [...] Notice that *++argv is a pointer to an argument string, so (*++argv)[0]
    is its first character. (An alternate valid form would be **++argv.) Because
    [] binds tighter than * and ++, the parentheses are necessary; without them
    the expression would be taken as *++(argv[0]). In fact, that is what we have
    used in the inner loop, where the task is to walk along a specific argument
    string. In the inner loop, the expression *++argv[0] increments the pointer
    argv[0]!" (p. 117)

    Now it seems that this solution (for "walk[ing] along a specific argument
    string" by using the construct "*++argv[0]") raised some criticism:

    "The sample program increments argv[0]. Although argv is modifiable, and
    argv[0][0] is modifiable (if argv[0] is not a null pointer), argv[0] is not
    modifiable. (At least, not always.)" (Peter Seebacher)

    "117 (ยง5.10): In the find example, the program increments argv[0]. This is not
    specifically forbidden, but not specifically allowed either." (Source:
    http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)

    Now here's my question: Would the following (slight modification of the
    original code) be a reasonable (or at least correct) solution?:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    while (--argc > 0 && (*++argv)[0] == '-') {
    char c, *arg = *argv;
    while (c = *++arg)
    switch (c) {
    case 'x':
    except = 1;
    break;
    case 'n':
    number = 1;
    break;
    default:
    printf("find: illegal option %c\n", c);
    argc = 0;
    found = -1;
    break;
    }
    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    G.

    --

    E-mail: info<at>simple-line<Punkt>de
    Gregor H., Apr 19, 2007
    #1
    1. Advertising

  2. Gregor H.

    Gregor H. Guest

    On Thu, 19 Apr 2007 13:38:07 +0200, Gregor H. <nomail@invalid> wrote:

    Slightly changed my code (for better agreement with the original one).

    >
    > in K&R (2nd edition) the authors published the following code for dealing with
    > command line arguments (p. 117):
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > while (--argc > 0 && (*++argv)[0] == '-')
    > while (c = *++argv[0])
    > switch (c) {
    > case 'x':
    > except = 1;
    > break;
    > case 'n':
    > number = 1;
    > break;
    > default:
    > printf("find: illegal option %c\n", c);
    > argc = 0;
    > found = -1;
    > break;
    > }
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > Now here's my question: Would the following (slight modification of the
    > original code) be a reasonable (or at least correct) solution?:
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > while (--argc > 0 && (*++argv)[0] == '-') {
    > char *arg = *argv;
    > while (c = *++arg)
    > switch (c) {
    > case 'x':
    > except = 1;
    > break;
    > case 'n':
    > number = 1;
    > break;
    > default:
    > printf("find: illegal option %c\n", c);
    > argc = 0;
    > found = -1;
    > break;
    > }
    > }
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >



    G.

    --

    E-mail: info<at>simple-line<Punkt>de
    Gregor H., Apr 19, 2007
    #2
    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. Lim Siew Cheng

    Re: Problematic Postbacks

    Lim Siew Cheng, Aug 12, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    376
    IbrahimMalluf
    Aug 12, 2003
  2. Duray AKAR

    Problematic Postbacks

    Duray AKAR, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    334
    Duray AKAR
    Aug 12, 2003
  3. Fraggle
    Replies:
    1
    Views:
    426
    Fraggle
    Nov 27, 2003
  4. Eric Lilja
    Replies:
    3
    Views:
    636
    kwikius
    Jul 1, 2006
  5. PerlNovice

    Problematic Perl code

    PerlNovice, Jan 2, 2007, in forum: Perl Misc
    Replies:
    8
    Views:
    110
    Eric Schwartz
    Jan 5, 2007
Loading...

Share This Page