I am unable to understand how char *argv[] is similar to char **argv. Also please let me know when to use pointers?
A function parameter declared as having array type is silently adjusted to having pointer type. Thus, if you declare a function with a parameter of type int x[]
, the parameter actually has type int *x
. Similarly, char *argv[]
in a function parameter is the same as char **argv
as the array of pointers is adjusted to a pointer to pointer.
Basically char * argv[]
is array of char pointers and char ** argv
is pointer to char pointer.
As we pass it as function parameter char * argv[]
is adjusted to pointer-type which points to initial element of that array, char ** argv
and are both the same things .
6.7.5.3 Function declarators (including prototypes) ...
7 A declaration of a parameter as ''array of type'' shall be adjusted to ''qualified pointer to type'', where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.
As you see we use both definitions of main
are equivalent -
int main(int argc, char ** argv)
And
int main(int argc, char *argv[])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.