How do pointer to pointers work in C?

前端 未结 14 1284
渐次进展
渐次进展 2020-11-22 02:03

How do pointers to pointers work in C? When would you use them?

14条回答
  •  太阳男子
    2020-11-22 02:45

    There so many of the useful explanations, but I didnt found just a short description, so..

    Basically pointer is address of the variable. Short summary code:

         int a, *p_a;//declaration of normal variable and int pointer variable
         a = 56;     //simply assign value
         p_a = &a;   //save address of "a" to pointer variable
         *p_a = 15;  //override the value of the variable
    
    //print 0xfoo and 15 
    //- first is address, 2nd is value stored at this address (that is called dereference)
         printf("pointer p_a is having value %d and targeting at variable value %d", p_a, *p_a); 
    

    Also useful info can be found in topic What means reference and dereference

    And I am not so sure, when can be pointers useful, but in common it is necessary to use them when you are doing some manual/dynamic memory allocation- malloc, calloc, etc.

    So I hope it will also helps for clarify the problematic :)

提交回复
热议问题