In a normal c/c++ program we write main function as either
int main(int c, char **argv)
or
int main(int c, char *argv[])
When used as a function parameter
char a[] // compiler interpret it as pointer to char
is equivalent to
char *a
and similarly, in main
's signature, char *argv[]
is equivalent to char **argv
. Note that in both of the cases char *argv[]
and char **argv
, argv
is of type char **
(not an array of pointers!).
The same is not true for the declaration
char **r;
char *a[10];
In this case, r
is of type pointer to pointer to char
while a
is of type array of pointers to char
.
The assignment
r = a; // equivalent to r = &a[0] => r = &*(a + 0) => r = a
is valid because in this expression again array type a
will be converted to pointer to its first element and hence of the type char **
.
Always remember that arrays and pointers are two different types. The pointers and arrays equivalence means pointer arithmetic and array indexing are equivalent.
Suggested reading:
argv
is an argument so the array is decayed to pointer and there is no way other than size (int c
) to differentiate.
When a double pointer and array of pointer are not the arguments, their syntax may look similar sometimes but their type is different and thus the compiler generates different types of code for both.
When the variable of interest is not the function argument, sizeof
will give different size for pointer to pointer and array of pointers.
Slightly related question: extern declaration, T* v/s T[]
Look!
Double pointers actually store like this
Let us say
int p,*q,**r;
p=50;
let the address of p
is 400
(&p
is 400
)
if we write q=p
and print q
we will get 400
as the answer since q
refers to the address of p
and *p
will render 50
as output since operator *
refers to "value at address".
Now, let us say q
has the address 500
(&q
outputs 500
) therefore when we do like this : r=q
the r
contains the value 500
and on prefixing r
with *
that is *r
the output shall be 400
because r
renders the value of q
which stores the address of p
being a pointer variable.
therefore,
if in a C program we run the following code
int main()
{
int p,*q,**r; //assume the address of p to be 400
//assume the address of q to be 500
p=50;
q=p;
r=q;
printf("Value of p-->%d",p);
printf("\nValue of q-->%d",q);
printf("\nValue at address of q \"in the address displayed above\"-->%d",*p);
printf("\nValue of r \"which will be the address of q\"-->%d",r);
printf("\nValue of r \"which will be the affffdress of p-->%d",*r);
printf("\nValue of r \"which will be the value of p\"-->%d",**r);
/*
Please change the format specifiers and replace the specifier from %d to %u in case the address value is not being displayed
*/
return 0;
}
OUTPUT
-------
Value of p--> 50
Value of q--> 400
Value at address of q "in the address displayed above"--> 50
Value of r "which will be the address of q"--> 500
Value of r "which will be the affffdress of p"--> 400
Value of r "which will be the value of p"--> 50
Through the above example, I just tried to explain the use of a double pointer. Perhaps you may know this Still I made the point vivid
Now utilizing the above example with array.
Look
Arrays are already pointers since they can be wither referenced as *(a+1) or a[1] .
So, double pointers may mean array of pointers or a string Array as per your question. The use of double pointer notations depends upon the situation.
In the question that you have posted above the _TCHAR**argv or simply char **argv is the array of characters for the console input which is always accepted as a stream of characters.
In java, we use something similar such as public static void main(String argv[])
This clearly shows that the main method accepts input which are the array of char arrays (or Strings to be little bit general).
Hope you have understood the difference. If not kindly comment. I will explain it to you.
Thank you