Previous      Next

```//              Pointers - indexing
//
// 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("Contents of d: %u\n",d);
printf("Address of location one up from d: %u\n",(&d+1));
printf("Contents of location one up from d: %u\n",*d);
printf("Contents of location one up from d: %u\n",*(&d+1));
printf("Contents of location one up from d: %u\n\n",**&d);

printf("Address of location two up from d: %u\n",(&d+2));
printf("Contents of location two up from d: %u\n",**d);
printf("Contents of location two up from d: %u\n",*(&d+2));
printf("Contents of location two up from d: %u\n",**(&d+1));
printf("Contents of location two up from d: %u\n\n",***&d);

printf("Address of location three up from d: %u\n",(&d+3));
printf("Contents of location three up from d: %u\n",***d);
printf("Contents of location three up from d: %u\n",*(&d+3));
printf("Contents of location three up from d: %u\n",**(&d+2));
printf("Contents of location three up from d: %u\n",***(&d+1));
printf("Contents of location three up from d: %u\n\n",****&d);

printf("Contents of location pointed to by location two up from d: %d\n",*(d+2));
printf("Contents of location pointed to by b: %d\n\n",*b);
}

// Sample Output:
//
//
//   Contents of d: 3221223436
//   Address of location one up from d: 3221223436
//   Contents of location one up from d: 3221223440
//   Contents of location one up from d: 3221223440
//   Contents of location one up from d: 3221223440
//
//   Address of location two up from d: 3221223440
//   Contents of location two up from d: 3221223444
//   Contents of location two up from d: 3221223444
//   Contents of location two up from d: 3221223444
//   Contents of location two up from d: 3221223444
//
//   Address of location three up from d: 3221223444
//   Contents of location three up from d: 10
//   Contents of location three up from d: 10
//   Contents of location three up from d: 10
//   Contents of location three up from d: 10
//   Contents of location three up from d: 10
//
//   Contents of location pointed to by location two up from d: 10
//   Contents of location pointed to by b: 10
```