Difference between double pointer and array of pointers

前端 未结 3 1012
花落未央
花落未央 2020-12-05 11:41

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


        
3条回答
  •  既然无缘
    2020-12-05 12:23

    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

提交回复
热议问题