468,463 Members | 2,036 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

char** argv versus char *argv[]

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.
Nov 17 '07 #1
8 15092
On Nov 17, 3:33 pm, oogie <cmadu...@gmail.comwrote:
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.
Nov 17 '07 #2
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.
Nov 17 '07 #3
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.
Nov 17 '07 #4
Thanks for pointing out that the code wouldn't compile, it definitely
cleared things up.
Nov 17 '07 #5
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
Nov 17 '07 #6
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
Nov 17 '07 #7
On Nov 17, 10:26 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
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
Nov 17 '07 #8
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


Nov 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Bret | last post: by
5 posts views Thread by jab3 | last post: by
10 posts views Thread by kevin.hall | last post: by
18 posts views Thread by Pedro Pinto | last post: by
9 posts views Thread by happyvalley | last post: by
24 posts views Thread by Logan | last post: by
12 posts views Thread by kevin.eugene08 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.