Previous      Next

//            Pointers - three levels of indirection                                        
//                                                                               
// Declaration: * means "pointer variable"                                     
//     Thus "int *b;" declares b to be a variable of type pointer to int       
//     "int **c;" declares c to be a variable of type pointer to pointer to int
// Non-declaration: * means contents of, & means address of                      
//     Thus "*b = 10;" means the int location pointed to by b gets value 10    
//     "b = &a;" means the contents of b is set to the address of variable a

void main()
{
   int a=10;
   int *b;
   int **c;
   int ***d;
   
   b = &a;
   c = &b;
   d = &c;
   printf("\nAddress of a: %u\n",&a);
   printf("Address of b: %u\n",&b);
   printf("Address of c: %u\n",&c);
   printf("Address of d: %u\n\n",&d);   
   printf("Contents of d: %u\n",d);
   printf("Contents of location pointed to by d: %u\n",*d);
   printf("Contents of location pointed to by location pointed to by d: %u\n",**d);
   printf("Contents of loc. -> by loc. -> by loc. -> d: %d\n",***d);   
}

// Sample Output:
//   Address of a: 3221223444
//   Address of b: 3221223440
//   Address of c: 3221223436
//   Address of d: 3221223432
//
//   Contents of d: 3221223436
//   Contents of location pointed to by d: 3221223440
//   Contents of location pointed to by location pointed to by d: 3221223444
//   Contents of loc. -> by loc. -> by loc. -> d: 10