Re: The type of argv in K&R2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Bacarisse

    Re: The type of argv in K&R2

    candide <candide@free.i nvalidwrites:
    K&R2 §5.10 tells that the second parameter to the main function,
    usually called argv, has type pointer to character strings.
    >
    Some quotes from the book :
    >
    -------------------- 8< ----------------------------------------
    "the second (argv, for argument vector) is a pointer to an array of
    character strings"
    >
    "Since argv is a pointer to an array of pointers,"
    >
    "Since argv is a pointer to the beginning of the array of argument strings"
    -------------------- >8 ----------------------------------------
    >
    In fact, argv is rather an array of strings, as explained in the
    preceding § :
    argv can't be an array. It is a function parameter and arrays can't
    be passed to functions in C. There is indeed an array "out there" but
    all main can ever see is a pointer to it.

    Not everyone likes the term "array of strings" -- it can be see as a
    bit woolly. What is not in question (I hope) is that, inside main,
    argv is not an array: it is a pointer to the first element of an array
    whose elements are character pointers. When main is called, a further
    guarantee is made: that these pointers (if they are not NULL) point to
    strings (i.e. that the character arrays pointed to are
    null-terminated).

    You will hear people say "argv is an array of strings" and that is fine
    at the bus stop (if you frequent that sort of bus stop) but only
    because everyone know what it really meant: that the second parameter
    passed to main is a pointer to an array of pointers that point to strings.

    --
    Ben.
  • Richard Heathfield

    #2
    Re: The type of argv in K&amp;R2

    candide said:
    Ben Bacarisse a ecrit :
    >
    >argv can't be an array.
    >
    >
    In K&R, the argv parameter is declared as :
    >
    char *argv[]
    >
    >
    so the type of argv is array to pointer to char.
    No, it's a pointer to pointer to char. It's just that it points to the
    first element in an array of pointers to char.

    And the C90 standard agrees :
    >
    ----------------------- 8< ---------------------------------
    5.1.2.2.1 Program startup
    >
    (...)the array members argv[0] through argv[argc-1] (...)
    That's fine - those pointers /are/ array members.
    (...)to by the argv array shall be (...)
    That isn't fine - it's sloppy wording. But we've been round this loop
    before, and the committee aren't about to change anything.

    K&R declares argv to be "a pointer to an array of pointers" and it seems
    to me to be incorrect.
    Sloppy wording again. But note that, just after your cite, K&R says very
    unsloppily that "argv... is a pointer to pointer to char", which is right.
    It /also/ says "pointer to the beginning of the array of argument
    strings". If we take "beginning of the array" as meaning "first member of
    the array", that's actually right too.

    But yes, what you've succeeded in noting is that both the Standard and K&R2
    are guilty of describing argv in rather woolly terms. It has been noted
    before, and no doubt it will be noted again.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Keith Thompson

      #3
      Re: The type of argv in K&amp;R2

      Richard Heathfield <rjh@see.sig.in validwrites:
      candide said:
      [...]
      (...)to by the argv array shall be (...)
      >
      That isn't fine - it's sloppy wording. But we've been round this loop
      before, and the committee aren't about to change anything.
      [...]

      I agree that it's sloppy, but a friendly reading (one that makes it
      correct) is possible.

      If you interpret the phrase "the argv array" to mean "the array to
      whose first element argv points", then the wording is correct. Note
      that this does *not* imply that argv is itself an array.

      But it's perfectly reasonable to intepret the phrase "the argv array"
      to mean "the array named ``argv''", and there is no such array
      (there's only a pointer named ``argv''). So yes, the wording is
      sloppy.

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      Working...