array to pointer decay question !!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • m sergei

    #1

    array to pointer decay question !!

    main(int argc, char *argv[])

    the second parameter to function main takes a pointer to an array.

    can we also write it in terms of a reference rather than a pointer ?
    an example of using (usage) as a reference would be helpful
  • Richard Bos

    #2
    Re: array to pointer decay question !!

    sergei_m_1@yaho o.com (m sergei) wrote:
    [color=blue]
    > main(int argc, char *argv[])
    >
    > the second parameter to function main takes a pointer to an array.
    >
    > can we also write it in terms of a reference rather than a pointer ?[/color]

    Of course not. References are C++, not C.

    Perhaps you want comp.lang.c++?

    Richard

    Comment

    • Thomas Matthews

      #3
      Re: array to pointer decay question !!

      m sergei wrote:[color=blue]
      > main(int argc, char *argv[])
      >
      > the second parameter to function main takes a pointer to an array.
      >
      > can we also write it in terms of a reference rather than a pointer ?
      > an example of using (usage) as a reference would be helpful[/color]
      No, references are C++.

      You can declare it as: char * * arg_list.

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • Default User

        #4
        Re: array to pointer decay question !!

        m sergei wrote:[color=blue]
        >
        > main(int argc, char *argv[])
        >
        > the second parameter to function main takes a pointer to an array.[/color]

        No it doesn't. It takes a pointer to a pointer to char. That's why an
        equivalent version is:

        int main (int argc, char **argv)

        [color=blue]
        > can we also write it in terms of a reference rather than a pointer ?[/color]

        Ask in comp.lang.c++ where they will tell you that it is not the same.




        Brian Rodenborn

        Comment

        • Dan Pop

          #5
          Re: array to pointer decay question !!

          In <40E2EA46.5C4B1 19C@boeing.com. invalid> Default User <first.last@boe ing.com.invalid > writes:
          [color=blue]
          >m sergei wrote:[color=green]
          >>
          >> main(int argc, char *argv[])
          >>
          >> the second parameter to function main takes a pointer to an array.[/color]
          >
          >No it doesn't. It takes a pointer to a pointer to char. That's why an
          >equivalent version is:
          >
          >int main (int argc, char **argv)[/color]

          However, the equivalent version is losing a semantic clue: that argv is
          supposed to point to the first pointer of an array of pointers. It makes
          no difference to the compiler, but it does make a difference to the
          human reader.

          Furthermore it is quite idiomatic to refer to a pointer to the first
          element of an array as a pointer to an array, as long as it is clear that
          its type is not pointer to array. Example from the C standard itself:

          2 The strcpy function copies the string pointed to by s2 (including
          the terminating null character) into the array pointed to by
          ^^^^^^^^^^^^^^^ ^^^^^^^^
          s1. If copying takes place between objects that overlap, the
          ^^
          behavior is undefined.

          The case of main being so well known, most advanced programmers prefer
          **argv because it is easier to type than *argv[]. However, in any other
          function interface, I would recommend *argv[] instead of **argv, due to
          the semantic clue I was talking above. It's much less of an issue when
          dealing with plain arrays, so I wouldn't bother with a[] instead of *p.

          Dan
          --
          Dan Pop
          DESY Zeuthen, RZ group
          Email: Dan.Pop@ifh.de

          Comment

          • Default User

            #6
            Re: array to pointer decay question !!

            Dan Pop wrote:[color=blue]
            >
            > In <40E2EA46.5C4B1 19C@boeing.com. invalid> Default User <first.last@boe ing.com.invalid > writes:
            >[color=green]
            > >m sergei wrote:[color=darkred]
            > >>
            > >> main(int argc, char *argv[])
            > >>
            > >> the second parameter to function main takes a pointer to an array.[/color]
            > >
            > >No it doesn't. It takes a pointer to a pointer to char. That's why an
            > >equivalent version is:
            > >
            > >int main (int argc, char **argv)[/color]
            >
            > However, the equivalent version is losing a semantic clue: that argv is
            > supposed to point to the first pointer of an array of pointers. It makes
            > no difference to the compiler, but it does make a difference to the
            > human reader.[/color]

            I agree with you there, I use the char *argv[] style.
            [color=blue]
            > Furthermore it is quite idiomatic to refer to a pointer to the first
            > element of an array as a pointer to an array, as long as it is clear that
            > its type is not pointer to array.[/color]

            But when we're talking about equivalent types, the syntax is more
            important than the semantics. If we were describing the usage of argv,
            then you'd be correct. I don't agree with you regarding the OP's
            question.




            Brian Rodenborn

            Comment

            • Dan Pop

              #7
              Re: array to pointer decay question !!

              In <40E318A0.CAAF9 458@boeing.com. invalid> Default User <first.last@boe ing.com.invalid > writes:
              [color=blue]
              >Dan Pop wrote:[color=green]
              >>
              >> In <40E2EA46.5C4B1 19C@boeing.com. invalid> Default User <first.last@boe ing.com.invalid > writes:
              >>[color=darkred]
              >> >m sergei wrote:
              >> >>
              >> >> main(int argc, char *argv[])
              >> >>
              >> >> the second parameter to function main takes a pointer to an array.
              >> >
              >> >No it doesn't. It takes a pointer to a pointer to char. That's why an
              >> >equivalent version is:
              >> >
              >> >int main (int argc, char **argv)[/color]
              >>
              >> However, the equivalent version is losing a semantic clue: that argv is
              >> supposed to point to the first pointer of an array of pointers. It makes
              >> no difference to the compiler, but it does make a difference to the
              >> human reader.[/color]
              >
              >I agree with you there, I use the char *argv[] style.
              >[color=green]
              >> Furthermore it is quite idiomatic to refer to a pointer to the first
              >> element of an array as a pointer to an array, as long as it is clear that
              >> its type is not pointer to array.[/color]
              >
              >But when we're talking about equivalent types, the syntax is more
              >important than the semantics. If we were describing the usage of argv,
              >then you'd be correct. I don't agree with you regarding the OP's
              >question.[/color]

              Then, you're not agreeing with the C standard itself, either, which uses
              the same "sloppy" terminology, as I have shown in the part you have so
              conveniently snipped from my post.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              Working...