Moderator note: This thread was split from Why do you need pointers to pointers?
It'a an array of pointers to pointers, so this can also be written as
int main ( int argc, char *argv[] )
Moderator note: This thread was split from Why do you need pointers to pointers?
It'a an array of pointers to pointers, so this can also be written as
int main ( int argc, char *argv[] )
Last edited by laserlight; 05-22-2013 at 08:28 PM.
Actually, no, argv is just a pointer to a pointer. The char **argv syntax is equivalent to the char *argv[] syntax, but while the latter hints that an array is involved, that need not be the case (if this were not the global main function).Originally Posted by rcgldr
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
From context, it must be, but that does not change the fact that argv is a pointer to pointer to char, not "an array of pointers to pointers".
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
to expand on this a bit, an array is a group of like-types items of a length known at compile time. a pointer is never an array. a pointer may point to the first element of an array, and in fact, a native array decays into a pointer to its first element when passed as an argument.
this is an array
this is a pointer to the first element of an array.Code:int foo[10]; // sizeof(foo) = 10 * sizeof(int)
this is an array of pointersCode:int *foo = new int[10]; // sizeof(foo) = sizeof(void*)
this is a pointer to the first element of an array of pointersCode:int *foo[10]; // sizeof(foo) = 10 * sizeof(void*)
pointers are generally the same size as a long int, but I don't think it's guaranteed to be so. I believe the standard allows for multiple sizes of pointers within an implementation.Code:int **foo = new int*[10]; // sizeof(foo) = sizeof(void*)
Last edited by Elkvis; 05-21-2013 at 01:52 PM.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I should have stated that argv is an array of pointers to char, where each pointer is a pointer to the first character of a string. There are (argc) strings, and (argc) pointers in argv[]. argv[0] points to the command that invoked the program (usually the name of the program) in the command line, argv[1] points to the first parameter of the command line, argv[2] points to the second parameter, and argv[argc-1] points to the last parameter. argv[argc] is defined to be NULL in some texts, but I don't know if this is implementation dependent.
You can find this alternate syntax for main in various web articles and text books. Example links:
The C Book — Arguments to main
Main function - Wikipedia, the free encyclopedia
Post #4 in this thread gives one example of truly using a pointer to a pointer (for Base).
Last edited by rcgldr; 05-21-2013 at 05:21 PM.
O_oargv[0] points to the name of the program in the command line
You aren't really wrong, but that also isn't guaranteed.
[Edit]
A good example might be command aliases under "Bash" on "Gnu/Linux" systems.
You'd most likely get the alias instead of the program name.
[/Edit]
If you actually need the name of the program as executed, you are much better off reaching beyond the standard library and paraphernalia.
Soma
I disagree you should have stated that, since it is wrong. argv is a pointer to pointer. It's value happens to be the address of a pointer to char. That pointer to char has a value equal to the address of the first element of an array of pointers to char.
Wrong. argv is a pointer to a pointer. It points at the first element of an array of argc pointers to char.
argv[0] points at the first element of an array of char. That array of char is some representation of the name of the program, which is interpreted as a zero-interpreted string. It is implementation defined whether that string is the actual command that invoked the platform (it can also be some symbolic expansion of that command, the name of the executable itself, the full path name of the executable itself, or -- if the implementation cannot populate argv[0] -- a string literal "").
argc is also allowed to be zero, in which case argv[0] is the NULL pointer.
If argc is greater than 1, the contents of argv[1] through to argv[argc-1] are implementation defined. It is a common convention they are related to command line parameters - bear in mind that not all programs are invoked from a command line. The standard mandates that argv[argc] is the NULL pointer.
There are web articles and text books that define argv as an array of pointers. I included links to two of these in my previous post. argv is an acronym for "argument vector", where vector means array. So your opinion is the people that wrote text books and articles stating that argv is an array of pointers are all wrong?
char **argv can be considered as the alternate syntax, in the same manner than an array of integers can be declared as a pointer to integer when passed as a parameter to a function.
Last edited by rcgldr; 05-22-2013 at 09:45 AM.
in C++, an array's size is known at compile time. if that statement isn't true when applied to a particular object, it's not an array. it's really that simple. when we say "pointer to an array," it simply means that the programmer knows that there are a known number of elements beginning where the pointer points. the compiler has no knowledge of this. it only knows about a pointer to one element.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
Whatever argv "means" can be argued, but it has nothing to do with its type.
Furthermore, Wikipedia means "argument vector," but never that argc is an array or vector of pointers.
It is a fact that argv is a pointer to a pointer.
Precisely so. Therefore, in the function signature, char ** argv and char * argv[] mean the exact same thing: pointer to pointer. The [] are only syntax sugar and really does not mean an array, one of the potential pitfalls of C (which C++ unfortunately inherited).char **argv can be considered as the alternate syntax, in the same manner than an array of integers can be declared as a pointer to integer when passed as a parameter to a function.
Yes, they are all wrong, if they phrased it in that way. The formal parameter conventionally named argv is not an array of pointers: argv is a pointer to a pointer, and a pointer to a pointer is not an array of pointers, even though an array of pointers may be converted to a pointer to a pointer. However, if we were to imagine that the global main function is called, then the actual argument that corresponds to argv could be an array of pointers. Consequently, your implied claim in post #15 that this is not "truly using a pointer to a pointer" is bogus.Originally Posted by rcgldr
This seems to indicate that you think that in this declaration, x is an array:Originally Posted by rcgldr
That is not so: x is a pointer to int. The declaration is equivalent to this in every way except spelling:Code:void foo(int x[]);
So, which is the "alternative syntax"? I don't think there is one: it is just a matter of style. One might prefer the former to hint that an array is expected as the actual argument. On the other hand, one might prefer the latter since the formal parameter is indeed a pointer.Code:void foo(int* x);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Since this discussion about the parameters of main() seems to be a bit off topic, perhaps it should be moved into it's own thread?
That's the way it's phrased in the first article I mentioned:Originally Posted by rcgldr
The C Book — Arguments to main
I've never seen a text book or article that specifically states that the argv parameter for main() is not an array of pointers as opposed to it can be optionally declared in main as a pointer to a pointer. How argv is declared in main doesn't matter, what determines argv's actual structure (array of pointers or pointer to pointer) is the the code that creates argv and then calls main, and all the text books and articles I've read that state how argv is created, state that it's is created as an array of pointers to the command string and parameter strings.
x could be an array or x could be a pointer. This can't be determined by it's usage as a parameter in a function call or prototype. Only the actual instance of the declaration of x would determine if it's an array such as
int x[256];
or a pointer such as
int * x;
Last edited by rcgldr; 05-22-2013 at 03:56 PM.
[Edit]
I could not help but also address this comment.How argv is declared in main doesn't matter, what determines argv's actual structure (array of pointers or pointer to pointer) is the the code that creates argv and then calls main, and all the text books and articles I've read that state how argv is created, state that it's is created as an array of pointers to the command string and parameter strings.
The type of `argv' as either `char ** argv' or `char *argv[]' from the parameter is still a pointer to a pointer. The underlying type, "actual structure", does not matter in any way. You, as in the `main' function, don't own the underlying variable; you get passed a pointer to a pointer: the address of the first element of an array of pointers which themselves point to null-terminated arrays of `char'.
[/Edit]
O_oThis can't be determined by it's usage as a parameter in a function call or prototype.
That's the only place it can be determined, and that `x' in the relevant prototype is always a pointer.
I don't know why you don't believe us; this isn't nearly as complicated or conditional as you are making it.
Soma
Code:void foo(int x[]){} // `x' is a pointer. int main() { int x[256]; foo(x); }Code:void foo(int * x){} // `x' is a pointer. int main() { int x[256]; foo(x); }Code:void foo(int x[]){} // `x' is a pointer. int main() { int * x; foo(x); }Code:void foo(int * x){} // `x' is a pointer. int main() { int * x; foo(x); }
Last edited by phantomotap; 05-22-2013 at 04:07 PM.
Since this discussion about the parameters of main() seems to be a bit off topic, perhaps it should be moved into it's own thread?
It's not me that you need to convince, it's the authors of text books and articles such as this one. Did you read this article?
The C Book — Arguments to main
Wiki's standard is that some citable reference should be given for any claim. Once again, I have yet to read any text book or article that specifically states that argv in main is not an array of pointers (as opposed to it can alternately declared as a pointer to pointer).
This is seems to be an issue of semantics. I think it's more helpful to someone new to C / C++ to explain how the actual instance of argv is created, as opposed to how it can be declared as a parameter to main(). Apparently the people that write the text books and articles I mentioned share the same opinion.
Last edited by rcgldr; 05-22-2013 at 04:18 PM.