·  C++ – Syntax – Pointers – To Pointer

 

- Info:

  - Pointer to Pointer holds memory address at which another Pointer is stored.

  - Declaration of constant pointer to constant pointer to constant unsigned long primitive type would look like this:

    const unsigned long * const * const cptcptcul = &cul;

 

- Example:

  - This example shows how to use Pointer to Pointer.

  - Create C++ Console Application Test.cpp.

 

    Test.cpp

    #include <stdio.h>

   

    int main () {

   

      int i                       = 90;

      int * const cpti            = &i;

      int * const * const cptcpti = &cpti; // cptcpti is constant pointer to constant pointer to int

   

      printf("%i\n", cpti[0]      ); // 90     

      printf("%i\n", cpti         ); // 2293620

      printf("%i\n", cptcpti[0]   ); // 2293620

      printf("%i\n", cptcpti[0][0]); // 90     

   

    }

 

- Example:

  - This example shows how to use nested Pointers to Pointers.

  - Create C++ Console Application Test.cpp.

 

    Test.cpp

    #include <stdio.h>

   

    int main () {

   

      int i            = 100;

      int *pti         = &i;       //pti       is pointer                                  to int.

      int **ptpti      = &pti;     //ptpti     is pointer to pointer                       to int.

      int ***ptptpti   = &ptpti;   //ptptpti   is pointer to pointer to pointer            to int.

      int ****ptptptpti= &ptptpti; //ptptptpti is pointer to pointer to pointer to poniter to int.

   

      printf("%i\n", i               ); // 100    

      printf("%i\n", pti[0]          ); // 100    

   

      printf("%i\n", pti             ); // 2293620

      printf("%i\n", ptpti[0]        ); // 2293620

      printf("%i\n", ptpti[0][0]     ); // 100    

   

      printf("%i\n", ptpti           ); // 2293616

      printf("%i\n", ptptpti[0]      ); // 2293616

      printf("%i\n", ptptpti[0][0]   ); // 2293620

      printf("%i\n", ptptpti[0][0][0]); // 100    

   

    }