Re: The type of argv in K&R2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Heathfield

    Re: The type of argv in K&R2

    candide said:
    Hi,
    >
    K&R2 §5.10 tells that the second parameter to the main function,
    usually called argv, has type pointer to character strings.
    I don't think it says so in exactly those words (and if it does, I'd argue
    that it's wrong). Certainly I can't find such wording on a cursory look
    through that section.

    Okay, here's the whole thing.

    There exists an array of char *. Zero (or more, but exactly argc) members
    of that array each point to the first character of a string that
    represents a command line argument. The next member, the [argc] member, is
    guaranteed to be NULL.

    argv, as received by main, is a pointer to the first element in that array.

    --
    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
  • Richard Heathfield

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

    candide said:
    Richard Heathfield a ecrit :
    >candide said:
    >>
    >>Hi,
    >>>
    >>K&R2 §5.10 tells that the second parameter to the main function,
    >>usually called argv, has type pointer to character strings.
    >>
    >I don't think it says so in exactly those words
    >
    Oops, i should have said :
    >
    "has type pointer to array of character strings."
    I can't find those words either.

    --
    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

    • Richard Heathfield

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

      candide said:
      Richard Heathfield a écrit :
      >
      >>>
      >>"has type pointer to array of character strings."
      >>
      >I can't find those words either.
      >>
      >
      The exact sentence is :
      >
      "the second (argv, for argument vector) is a pointer to an array of
      character strings that contain the arguments, one per string."
      But that doesn't mention type at all, does it? Check the OP's Subject line
      (that's you, right?). The type of argv is char **.

      --
      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

      • Joe Wright

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

        Richard Heathfield wrote:
        candide said:
        >
        >Hi,
        >>
        >K&R2 §5.10 tells that the second parameter to the main function,
        >usually called argv, has type pointer to character strings.
        >
        I don't think it says so in exactly those words (and if it does, I'd argue
        that it's wrong). Certainly I can't find such wording on a cursory look
        through that section.
        >
        Okay, here's the whole thing.
        >
        There exists an array of char *. Zero (or more, but exactly argc) members
        of that array each point to the first character of a string that
        represents a command line argument. The next member, the [argc] member, is
        guaranteed to be NULL.
        >
        Surely there are argc+1 members. Given argc == 2, argv[2] is the third
        (and last) pointer in the array (and a NULL pointer).
        argv, as received by main, is a pointer to the first element in that array.
        >

        --
        Joe Wright
        "Everything should be made as simple as possible, but not simpler."
        --- Albert Einstein ---

        Comment

        • Richard Heathfield

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

          Joe Wright said:
          Richard Heathfield wrote:
          >candide said:
          >>
          >>Hi,
          >>>
          >>K&R2 §5.10 tells that the second parameter to the main function,
          >>usually called argv, has type pointer to character strings.
          >>
          >I don't think it says so in exactly those words (and if it does, I'd
          >argue that it's wrong). Certainly I can't find such wording on a cursory
          >look through that section.
          >>
          >Okay, here's the whole thing.
          >>
          >There exists an array of char *. Zero (or more, but exactly argc)
          >members of that array each point to the first character of a string that
          >represents a command line argument. The next member, the [argc] member,
          >is guaranteed to be NULL.
          >>
          Surely there are argc+1 members.
          Right. Where did I say different?

          Allow me to translate:

          There exists an array of char *. Zero (or more, but exactly argc)
          members of that array each point to the first character of a string that
          represents a command line argument. The next member, the [argc] member,
          is guaranteed to be NULL.

          Get it now? :-)
          Given argc == 2, argv[2] is the third
          (and last) pointer in the array (and a NULL pointer).
          Right. So exactly argc of them point to the first character of a string
          that represents a command line argument (and I suppose I should clarify
          that I am counting the program name - or some representation thereof - as
          a command line argument), and the argv[argc]th member, argv[2] in your
          example, is a null pointer. If that isn't broadly what I said the first
          time, please point out the discrepancy.

          <snip>

          --
          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

          • Richard Heathfield

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

            candide said:
            Richard Heathfield a ecrit :
            >
            >>The exact sentence is :
            >>>
            >>"the second (argv, for argument vector) is a pointer to an array of
            >>character strings that contain the arguments, one per string."
            >>
            >But that doesn't mention type at all, does it?
            >
            So what does it mention ?
            It mentions what argv /is/ (albeit rather imprecisely), not what /type/ it
            has. The type is char **.
            What the "pointer to an array of character
            strings" type is referring to ?
            You keep introducing the word "type" where it doesn't quite belong. If you
            want to know the type of argv, it's char **, or "pointer to pointer to
            char" in not-quite-English.

            The phrase "pointer to an array of character strings" is ambiguous in type
            terms because the array could be either of char arrays or of char
            pointers, and in any case a pointer to an array (which argv is not) is not
            the same thing as a pointer to the first element in an array (which argv
            is).
            >
            The type of argv is char **.
            >
            Which rules do you follow to find the type a function parameter is ?
            C rules.
            The C90 standard says :
            >
            ---------------------- 8< ----------------------------------
            6.5.4.3 Function declarators (including prototypes)
            (...)
            A parameter type list specifies the types of, and may declare
            identifiers for, the parameters of the function.
            ---------------------- >8 ----------------------------------
            Right. Note that, according to C rules, char **argv has type char **. Note,
            too, that within the context of a formal parameter list this type can
            equally be expressed char *[] (e.g. char *argv[]), but that doesn't stop
            it meaning "pointer to pointer to char".

            --
            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

            • Richard Heathfield

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

              candide said:
              Richard Heathfield a ecrit :
              >
              >>
              >It mentions what argv /is/ (albeit rather imprecisely),
              >
              >
              Good news! the Standard answers ontological question!!! ;)
              >
              >
              It mentions what argv /is/ (albeit rather imprecisely), not what
              >/type/ it has.
              >
              Paraphrasing the standard, char *argv[] declares the identifier argv and
              specifies the type of the argv parameter of the function main().
              >
              >
              has. The type is char **.
              >
              According to which C rules ?
              3.5.4.3 of C89 says, in part:

              For each parameter declared with function or array type, its type for these
              comparisons is the one that results from conversion to a pointer type, as
              in $3.7.1.

              3.7.1 of C89 says, in part:

              A declaration of a parameter as ``array of type '' shall be adjusted to
              ``pointer to type ,''

              Thus, the declaration char *argv[], which would otherwise be "array of
              pointer to char", is adjusted to "pointer to pointer to char".

              Similar wording can be found in C99.

              --
              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

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

                candide <candide@free.i nvalidwrites:
                Richard Heathfield a écrit :
                >
                >
                "has type pointer to array of character strings."
                I can't find those words either.
                >
                The exact sentence is :
                >
                "the second (argv, for argument vector) is a pointer to an array of
                character strings that contain the arguments, one per string."
                [...]

                Assuming that's a correct excerpt from K&R2, I'd argue that it's
                inaccurate, or at the very least imprecise.

                There's arguably no such thing as an "array of character strings". A
                "string" is "a contiguous sequence of characters terminated by and
                including the first null character" (C99 7.1.1p1; I believe it's
                similar or identical in C90). It's a data *format*, not a data
                *type*.

                If you had an array of character arrays, where each character array
                contains a string:

                char arr[4][4] = {
                "aaa",
                "bbb",
                "ccc",
                "ddd" };

                then you might reasonably call this an "array of strings" -- but
                that's not what argv points to. argv points to the first element of
                an array of pointers, where each pointer (except the last, which is
                null) is a pointer to a string. And we can use the phrase "pointer to
                a string" only because the standard specifically defines it as "a
                pointer to its initial (lowest addressed) character" (C99 7.1.1p1).

                --
                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

                • Joe Wright

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

                  Richard Heathfield wrote:
                  Joe Wright said:
                  >
                  >Richard Heathfield wrote:
                  >>candide said:
                  >>>
                  >>>Hi,
                  >>>>
                  >>>K&R2 §5.10 tells that the second parameter to the main function,
                  >>>usually called argv, has type pointer to character strings.
                  >>>
                  >>I don't think it says so in exactly those words (and if it does, I'd
                  >>argue that it's wrong). Certainly I can't find such wording on a cursory
                  >>look through that section.
                  >>>
                  >>Okay, here's the whole thing.
                  >>>
                  >>There exists an array of char *. Zero (or more, but exactly argc)
                  >>members of that array each point to the first character of a string that
                  >>represents a command line argument. The next member, the [argc] member,
                  >>is guaranteed to be NULL.
                  >>>
                  >Surely there are argc+1 members.
                  >
                  Right. Where did I say different?
                  >
                  Allow me to translate:
                  >
                  There exists an array of char *. Zero (or more, but exactly argc)
                  members of that array each point to the first character of a string that
                  represents a command line argument. The next member, the [argc] member,
                  is guaranteed to be NULL.
                  >
                  Get it now? :-)
                  >
                  >Given argc == 2, argv[2] is the third
                  >(and last) pointer in the array (and a NULL pointer).
                  >
                  Right. So exactly argc of them point to the first character of a string
                  that represents a command line argument (and I suppose I should clarify
                  that I am counting the program name - or some representation thereof - as
                  a command line argument), and the argv[argc]th member, argv[2] in your
                  example, is a null pointer. If that isn't broadly what I said the first
                  time, please point out the discrepancy.
                  >
                  <snip>
                  >
                  I didn't read carefully enough. You are perfectly right. Sorry for the
                  intrusion.

                  --
                  Joe Wright
                  "Everything should be made as simple as possible, but not simpler."
                  --- Albert Einstein ---

                  Comment

                  Working...