array to pointer decay question !!

Discussion in 'C Programming' started by m sergei, Jun 30, 2004.

  1. m sergei

    m sergei Guest

    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
    m sergei, Jun 30, 2004
    #1
    1. Advertising

  2. m sergei

    Richard Bos Guest

    (m sergei) wrote:

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


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

    Perhaps you want comp.lang.c++?

    Richard
    Richard Bos, Jun 30, 2004
    #2
    1. Advertising

  3. m sergei wrote:
    > 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

    No, references are C++.

    You can declare it as: char * * arg_list.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jun 30, 2004
    #3
  4. m sergei

    Default User Guest

    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)


    > can we also write it in terms of a reference rather than a pointer ?


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




    Brian Rodenborn
    Default User, Jun 30, 2004
    #4
  5. m sergei

    Dan Pop Guest

    In <> Default User <> writes:

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


    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, Jun 30, 2004
    #5
  6. m sergei

    Default User Guest

    Dan Pop wrote:
    >
    > In <> Default User <> writes:
    >
    > >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)

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


    I agree with you there, I use the char *argv[] style.

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


    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
    Default User, Jun 30, 2004
    #6
  7. m sergei

    Dan Pop Guest

    In <> Default User <> writes:

    >Dan Pop wrote:
    >>
    >> In <> Default User <> writes:
    >>
    >> >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)

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

    >
    >I agree with you there, I use the char *argv[] style.
    >
    >> 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.

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


    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, Jul 1, 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. Mantorok Redgormor

    array decay

    Mantorok Redgormor, Sep 23, 2003, in forum: C Programming
    Replies:
    1
    Views:
    395
    Kevin Bracey
    Sep 23, 2003
  2. Mantorok Redgormor

    array decay

    Mantorok Redgormor, Oct 8, 2003, in forum: C Programming
    Replies:
    2
    Views:
    438
    Barry Schwarz
    Oct 9, 2003
  3. pandapower

    array to pointer decay question !!

    pandapower, Feb 2, 2004, in forum: C Programming
    Replies:
    5
    Views:
    571
    Horst Kraemer
    Feb 2, 2004
  4. frederik aa de jonge

    module for calculations involving radioactive decay?

    frederik aa de jonge, Nov 2, 2006, in forum: Python
    Replies:
    0
    Views:
    405
    frederik aa de jonge
    Nov 2, 2006
  5. Snis Pilbor

    Question about function decay

    Snis Pilbor, Aug 4, 2006, in forum: C Programming
    Replies:
    5
    Views:
    279
    Flash Gordon
    Aug 5, 2006
Loading...

Share This Page