char** argv versus char *argv[]

Discussion in 'C++' started by oogie, Nov 17, 2007.

  1. oogie

    oogie Guest

    Hi,

    The arguments to main are usually passed as char* argv[] or char **
    argv, where for this example argv is the array of arguments. What I
    don't understand is how accessing the actual argument value evaluates
    to the following:

    int main(int argc, char** argv)
    {
    char ch = argv[0];
    }

    or

    int main(int argc, char *argv[])
    {
    char *ch = argv[0];
    }

    I do get the logic behind the latter case, but char** argv really
    puzzles. Any help would be appreciated.
     
    oogie, Nov 17, 2007
    #1
    1. Advertising

  2. * oogie:
    > Hi,
    >
    > The arguments to main are usually passed as char* argv[] or char **
    > argv, where for this example argv is the array of arguments. What I
    > don't understand is how accessing the actual argument value evaluates
    > to the following:
    >
    > int main(int argc, char** argv)
    > {
    > char ch = argv[0];
    > }


    Have you tried putting this to your compiler?

    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 17, 2007
    #2
    1. Advertising

  3. oogie

    werasm Guest

    On Nov 17, 3:33 pm, oogie <> wrote:
    > Hi,
    >
    > The arguments to main are usually passed as char* argv[] or char **
    > argv, where for this example argv is the array of arguments. What I
    > don't understand is how accessing the actual argument value evaluates
    > to the following:
    >
    > int main(int argc, char** argv)
    > {
    > char ch = argv[0];
    > }


    Besides not compiling, I can comment on this.

    - char[] decays to char* during a function call.

    In this case:
    int main( int argc, char* argv[] ){}

    is equivalent to this:
    int main( int argc, char** ){ }

    The reason for this is explained in Section 13.1 of c++98 standard:
    "Parameter declarations that differ only in pointer * versus array[]
    are equivalent"

    Therefore this is exactly the same - in both cases an array
    of NULL terminated strings.

    Regards,

    Werner





    >
    > or
    >
    > int main(int argc, char *argv[])
    > {
    > char *ch = argv[0];
    >
    > }
    >
    > I do get the logic behind the latter case, but char** argv really
    > puzzles. Any help would be appreciated.
     
    werasm, Nov 17, 2007
    #3
  4. oogie

    Ron Natalie Guest

    oogie wrote:
    > Hi,
    >
    > The arguments to main are usually passed as char* argv[] or char **
    > argv,


    As far as the type of a function is concerned an array is always
    converted to a pointer to the first element. The type of main
    (DESPITE the text of the standard) here is int main(int, char**).

    The difference between the two forms just like the presence or
    absence of top level const doesn't matter external to the function
    just internal.
     
    Ron Natalie, Nov 17, 2007
    #4
  5. oogie

    Ron Natalie Guest

    werasm wrote:

    > The reason for this is explained in Section 13.1 of c++98 standard:
    > "Parameter declarations that differ only in pointer * versus array[]
    > are equivalent"
    >

    That's for overloading (main is one of the only function in C++ that's
    inversely overloaded....i.e., you give one and the implemenation shapes
    the call to match).

    The important clause is 8.3.5 (3) where parameters and return types of
    type array of are converted to pointer to determine the type of the
    function.
     
    Ron Natalie, Nov 17, 2007
    #5
  6. oogie

    oogie Guest

    Thanks for pointing out that the code wouldn't compile, it definitely
    cleared things up.
     
    oogie, Nov 17, 2007
    #6
  7. Ron Natalie wrote:
    >> ...
    >> The arguments to main are usually passed as char* argv[] or char **
    >> argv,

    >
    > As far as the type of a function is concerned an array is always
    > converted to a pointer to the first element. The type of main
    > (DESPITE the text of the standard) here is int main(int, char**).
    >
    > The difference between the two forms just like the presence or
    > absence of top level const doesn't matter external to the function
    > just internal.


    Err... And what would be the _internal_ difference between a 'char*[]' parameter
    and a 'char **' parameter?

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 17, 2007
    #7
  8. werasm wrote:
    > ...
    > - char[] decays to char* during a function call.
    >
    > In this case:
    > int main( int argc, char* argv[] ){}
    >
    > is equivalent to this:
    > int main( int argc, char** ){ }
    > ...


    Strictly speaking, this has absolutely nothing to do with any function calls.
    When used in the function parameter declaration, declarator 'T p[N]' is
    equivalent to the 'T p[]' and is equivalent to the 'T* p'. One can think of them
    as "decaying" into each other at purely syntactical level. No "calls" necessary.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 17, 2007
    #8
  9. oogie

    werasm Guest

    On Nov 17, 10:26 pm, Andrey Tarasevich <>
    wrote:
    > werasm wrote:
    > > ...
    > > - char[] decays to char* during a function call.

    >
    > > In this case:
    > > int main( int argc, char* argv[] ){}

    >
    > > is equivalent to this:
    > > int main( int argc, char** ){ }
    > > ...

    >
    > Strictly speaking, this has absolutely nothing to do with any function calls.
    > When used in the function parameter declaration, declarator 'T p[N]' is
    > equivalent to the 'T p[]' and is equivalent to the 'T* p'. One can think of them
    > as "decaying" into each other at purely syntactical level. No "calls" necessary.


    Sorry, rephrase:

    - char[] decays to char* during a function declaration. Is this
    better?

    My main point was to indicate to the OP that in a function
    declaration a parameter "char [] or char [N]" does not have
    the same type as char [N], but has the type char* (or decays
    to char*). I should not have said "during a call" as this
    was not what I meant.

    As a small example:

    void foo( char param[] )
    {
    ++param; //Compiles fine...

    char array[10];
    ++array; //Fails to compile!
    }

    Regards,

    Werner
     
    werasm, Nov 17, 2007
    #9
  10. oogie

    Default User Guest

    werasm wrote:


    > Sorry, rephrase:
    >
    > - char[] decays to char* during a function declaration. Is this
    > better?


    No. The two forms are equivalent, there is no conversion or "decay".
    That is something that happens when a function is called, not when
    declared. Actually, it's that the name of an array is implicitly
    converted to a pointer in most cases, exceptions including the
    address-of operator and the sizeof operator.

    If a size is included in a declaration, it's ignored, so:

    void f(char *p);
    void f(char p[]);
    void f(char p1000]);

    Are all equivalent declarations.

    The only other place char[] is legal is when used as a declaration with
    an initializer, no conversion there either.




    Brian
     
    Default User, Nov 18, 2007
    #10
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,695
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,072
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. jab3

    char **argv & char *argv[]

    jab3, Dec 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    719
    Chris Torek
    Dec 8, 2004
  5. Paul Butcher
    Replies:
    12
    Views:
    777
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page