17

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[])

Here argv represents an array of pointers but we even represent double pointer(pointer to pointer) using **.

ex:

char p,*q,**r;
q=&p;
r=&q;

Here r is a double pointer and not array of pointers.

Can any one explain the difference?

Vanarajan
  • 973
  • 1
  • 10
  • 35
Rakesh kumar
  • 392
  • 5
  • 13
  • I have found this answer here [ 1. First answer](http://stackoverflow.com/questions/18273370/the-correct-way-to-initialize-a-dynamic-pointer-to-a-multidimensional-array?answertab=oldest#tab-top) [ 2. Second answer](http://stackoverflow.com/questions/16001803/pointer-to-pointer-dynamic-two-dimensional-array?answertab=oldest#tab-top) –  Nov 03 '15 at 06:02
  • A "double pointer" is not a thing in C or C++. What you're playing with here is a "pointer to pointer". – mlp Jul 15 '19 at 16:22

3 Answers3

11

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:

haccks
  • 104,019
  • 25
  • 176
  • 264
2

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[]

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • @mrtimdog Thanks for the comment. I invite you to edit the answer or point out the places where it needs edit. – Mohit Jain Nov 07 '15 at 03:30
1

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 adddress 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 adddress 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

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Aniruddha Sinha
  • 799
  • 1
  • 10
  • 22
  • This post has formatting problems, that is, the stars (`*`) that you used for pointers have been interpreted as Markdown for italic or bold. Your first paragraph is entirely in bold. Please edit your post and fix the error. The easiest way is probably to wrap all your pointers in code formatting (ctrl-k), so that inside code block italic and bold are disabled. – Fabio says Reinstate Monica Nov 03 '15 at 15:20
  • The first paragraph has been intentionally kept bold so as to focus every ones attention over the core concepts of double pointer and talking about the code block: The code might contain few printing errors when you run because i have not checked or run the code. I have just used my concepts which are correct to my knowledge. Regarding the formatting problem, I dont see any problems in the code block – Aniruddha Sinha Nov 04 '15 at 05:15
  • I will try to focus your attention onto a comment in the code block "Please change the format specifiers and replace the specifier from %d to %u in case the address value is not being displayed" – Aniruddha Sinha Nov 04 '15 at 05:17