Why is argv
declared as "a pointer to pointer to the first index of the array", rather than just being "a pointer to the first index of array" (char* argv
)?
Why is the notion of "pointer to pointer" required here?
6 Answers 6
Argv is basically like this:
On the left is the argument itself--what's actually passed as an argument to main. That contains the address of an array of pointers. Each of those points to some place in memory containing the text of the corresponding argument that was passed on the command line. Then, at the end of that array there's guaranteed to be a null pointer.
Note that the actual storage for the individual arguments are at least potentially allocated separately from each other, so their addresses in memory might be arranged fairly randomly (but depending on how things happen to be written, they could also be in a single contiguous block of memory--you simply don't know and shouldn't care).
-
55Whatever layout engine drew that diagram for you has a bug in their minimize-crossings algorithm!Eric Lippert– Eric Lippert2019年01月20日 17:17:07 +00:00Commented Jan 20, 2019 at 17:17
-
45@EricLippert Could be intentional to emphasize that that the pointees might not be contiguous nor in order.jamesdlin– jamesdlin2019年01月20日 20:55:49 +00:00Commented Jan 20, 2019 at 20:55
-
3I would say it's intentionalMichael– Michael2019年01月20日 22:36:53 +00:00Commented Jan 20, 2019 at 22:36
-
25It was certainly intentional--and I'd guess Eric probably figured that, but (correctly, IMO) thought the comment was funny anyway.Jerry Coffin– Jerry Coffin2019年01月21日 02:57:58 +00:00Commented Jan 21, 2019 at 2:57
-
3@JerryCoffin, one might also point out that even if the actual arguments were contiguous in memory, they can have arbitrary lengths, so one would still need distinct pointers for each of them to be able to access
argv[i]
without scanning through all the previous ones.ilkkachu– ilkkachu2019年01月21日 11:41:38 +00:00Commented Jan 21, 2019 at 11:41
Because that's what the operating system provides :-)
Your question is a little bit of a chicken/egg inversion issue. The problem is not to choose what you want in C++, the problem is how you say in C++ what the OS is giving you.
Unix passes an array of "strings", each string being a command argument. In C/C++, a string is a "char*", so an array of strings is char* argv[], or char** argv, according to taste.
-
13No, it's exactly "the problem to choose what you want in C++". Windows, for example, provides the command line as a single string, and yet C/C++ programs still receive their
argv
array — the runtime takes care of tokenizing the command line and building theargv
array at the startup.Joker_vD– Joker_vD2019年01月20日 10:46:40 +00:00Commented Jan 20, 2019 at 10:46 -
15@Joker_vD I think in a twisted way it is about what the OS gives you. Specifically: I guess C++ did it this way because C did it this way, and C did it this way because at the time C and Unix were so inextricably linked and Unix did it this way.Daniel Wagner– Daniel Wagner2019年01月20日 15:33:37 +00:00Commented Jan 20, 2019 at 15:33
-
1@DanielWagner: Yes, this is from C's Unix heritage. On Unix / Linux a minimal
_start
that callsmain
just needs to passmain
a pointer to the existingargv
array in memory; it's already in the right format. The kernel copies it from the argv argument to theexecve(const char *filename, char *const argv[], char *const envp[])
system call that was made to start a new executable. (On Linux, argv[] (the array itself) and argc are on the stack on process entry. I assume most Unixes are the same, because that's a good place for it.)Peter Cordes– Peter Cordes2019年01月20日 20:38:30 +00:00Commented Jan 20, 2019 at 20:38 -
8But Joker's point here is that the C / C++ standards leave it up to the implementation where the args come from; they don't have to be straight from the OS. On an OS that passes a flat string, a good C++ implementation should include tokenizing, instead of setting
argc=2
and passing the whole flat string. (Following the letter of the standard is not sufficient to be useful; it intentionally leaves a lot of room for implementation choices.) Although some Windows programs will want to treat quotes specially, so real implementations do provide a way to get the flat string, too.Peter Cordes– Peter Cordes2019年01月20日 20:42:48 +00:00Commented Jan 20, 2019 at 20:42 -
1Basile's answer is pretty much this + @Joker's correction and my comments, with more details.Peter Cordes– Peter Cordes2019年01月20日 20:50:15 +00:00Commented Jan 20, 2019 at 20:50
First, as a parameter declaration, char **argv
is the same as char *argv[]
; they both imply a pointer to (an array or set of one or more possible) pointer(s) to strings.
Next, if you only have "pointer to char" — e.g. just char *
— then in order to access the nth item, you'll have to scan the first n-1 items to find the nth item's start. (And this would also impose the requirement that each of the strings are stored contiguously.)
With the array of pointers, you can directly index the nth item — so (while not strictly necessary — assuming the strings are contiguous) it is generally much more convenient.
To illustrate:
./program hello world
argc = 3
argv[0] --> "./program0円"
argv[1] --> "hello0円"
argv[2] --> "world0円"
It is possible that, in an os provided array of characters:
"./program0円hello0円world0円"
argv[0] ^
argv[1] ^
argv[2] ^
if argv were just a "pointer to char" you might see
"./program0円hello0円world0円"
argv ^
However (though likely by design of the os) there is no real guarantee that the three strings "./program", "hello", and "world" are contiguous. Further, this kind of "single pointer to multiple contiguous strings" is a more unusual data type construct (for C), especially compared with array of pointers to string.
-
what if instead of ,
argv --> "hello0円world0円"
you haveargv --> index 0 of the array
(hello), just like a normal array. why isn't this doable? then you keep reading the arrayargc
times. then you pass argv itself and not a pointer to argv.a user– a user2019年01月20日 01:22:10 +00:00Commented Jan 20, 2019 at 1:22 -
@auser, that's what argv --> "./program0円hello0円\world0円" is: a pointer to the first char (i.e. the ".") If you take that pointer past the first 0,円 then you have a pointer to "hello0円", and after that to "world0円". After argc times (hitting 0円"), you're done. Sure, it can be made to work, and as I said, an unusual construct.Erik Eidt– Erik Eidt2019年01月20日 07:16:11 +00:00Commented Jan 20, 2019 at 7:16
-
You forgot to state that in your example
argv[4]
isNULL
Basile Starynkevitch– Basile Starynkevitch2019年01月20日 07:50:50 +00:00Commented Jan 20, 2019 at 7:50 -
3There is a guarantee that (at least initially)
argv[argc] == NULL
. In this case that'sargv[3]
, notargv[4]
.Miral– Miral2019年01月21日 06:22:15 +00:00Commented Jan 21, 2019 at 6:22 -
1@Hill, yes, thank you as I was trying to be explicit about the null character terminators (and missed that one).Erik Eidt– Erik Eidt2019年01月21日 20:44:54 +00:00Commented Jan 21, 2019 at 20:44
Why C/C++ main argv is declared as "char* argv[]"
A possible answer is because the C11 standard n1570 (in §5.1.2.2.1 Program startup) and the C++11 standard n3337 (in §3.6.1 main function) require that for hosted environments (but notice that the C standard mentions also §5.1.2.1 freestanding environments) See also this.
The next question is why did the C and C++ standards choose main
to have such a int main(int argc, char**argv)
signature? The explanation is largely historical: C was invented with Unix, which has a shell which does globbing before doing fork
(which is a system call to create a process) and execve
(which is the system call to execute a program), and that execve
transmits an array of string program arguments and is related to the main
of the executed program. Read more about the Unix philosophy and about ABIs.
And C++ tried hard to follow the conventions of C and be compatible with it. It could not define main
to be incompatible with C traditions.
If you designed an operating system from scratch (still having a command line interface) and a programming language for it from scratch, you'll be free to invent different program starting conventions. And other programming languages (e.g. Common Lisp or Ocaml or Go) have different program starting conventions.
In practice, main
is invoked by some crt0 code. Notice that on Windows the globbing may be done by each program in the equivalent of crt0, and some Windows programs can start thru the non-standard WinMain entry point. On Unix, globbing is done by the shell (and crt0
is adapting the ABI, and the initial call stack layout that it has specified, to calling conventions of your C implementation).
Rather than thinking of it as "pointer to pointer", it helps to think of it as "array of strings", with []
denoting array and char*
denoting string. When you run a program, you can pass it one or more command-line arguments and these are reflected in the arguments to main
: argc
is the count of arguments and argv
lets you access individual arguments.
-
2+1 This! In many languages - bash, PHP, C, C++ - argv is an array of strings. Of this you have to think when you see
char **
orchar *[]
, which is the same.rexkogitans– rexkogitans2019年01月20日 15:08:01 +00:00Commented Jan 20, 2019 at 15:08
In many cases the answer is "because it's a standard". To quote C99 standard:
— If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup.
Of course, before it has been standardized it was already in use by K&R C in early Unix implementations, with the purpose of storing command-line parameters (something you have to care in Unix shell such as /bin/bash
or /bin/sh
but not in embedded systems). To quote first edition of K&R's "The C Programming Language" (pg. 110):
The first (conventionally called argc) is the number of command-line arguments the program was invoked with; the second (argv) is a pointer to an array of character strings that contain the arguments, one per string.
char* argv[]
orchar**
. That's a pointer to a pointer to a character; specifically the outer pointer points to the first pointer in an array, and the inner pointers point to the first characters of nul-terminated strings. There's no indices involved here.char* argv[]
puts the space in the wrong place. Saychar *argv[]
, and now it is clear that this means "the expression*argv[n]
is a variable of typechar
". Don't get caught up in trying to work out what's a pointer and what's a pointer to a pointer, and so on. The declaration is telling you what operations you can perform on this thing.char * argv[]
to the similar C++ constructstd::string argv[]
, and it might be easier to parse. ...Just don't start actually writing it that way!char &func(int);
which doesn't make&func(5)
have typechar
.