472,798 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 software developers and data experts.

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 15408
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
14
by: Hal Styli | last post by:
Is this a style thing? int main(int argc, char *argv ) or int main(int argc, char **argv ) i.e. *argv or **argv Why choose the latter?
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
10
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
by: happyvalley | last post by:
I just wonder how to pass arguments to this function with a char** void oldmain(int argv, char**argc) { ........ } void main(void) { int argv;
7
by: Jack | last post by:
Hi, I am having some problems with char and string datatypes. Here is an example int main(){ string str; cout << "enter string: "; cin >str; }
24
by: Logan | last post by:
Why (I) works but (II) gives segmentation error? (I) int main(int argc, char *argv) { printf("%s", argv); } (II) int main(int argc, char *argv) { printf("%s", argv);
12
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.