I've read the libc reference for int main (int argc, char *argv[]), as well as section 3.6.1 "Main function" in the current working standard of the C++ ISO documentation. I've also read a bunch about references. I understand they cannot be reassigned, that they must be only one layer deep, etc.
That said, why is the standard int main (int argc, char *argv[]) rather than int main (int argc, char * &argv) such that it is an "array"/data block holding the references to the parameters?
What I mean by this is why have an array of arrays (char **argv) that are NOT owned by a program and COULD be changed/moved during run time instead of memory that by its definition cannot be modified without the program's consent and proper handling (for example via signaling)? What am I missing?
First of, consider char* to mean c_string, and it immediately becomes obvious why you would need int main(int argc, c_string argv[]) versus int main(int argc, c_string& argv). After all, programs can take more than one parameter.
Since an array of references (if it were allowed) would turn out to hold only a single char per index, since references cannot be used to find the next character (without taking the reference of it, and thus converting it to a pointer), this does not make any sense either.
The assumption that the array of arguments is not owned by the program is simply false. The C standards 1999 and 2011 explicitly say:
The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
And my copy of the C++1y standard draft says nothing to the contrary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With