Pointer Examples

You should make certain that you know what will be output and why what is output is output by the following program. This type of code will probably come back to haunt you at some point in time (possibly even this semester.)
#include <stdio.h>

void swap(int *, int *);
void swap_ptr(int **, int **);

int main(void)
{
  int one = 11, two = 22;
  int *ptr1, *ptr2;

  ptr1 = &one;
  ptr2 = &two;

  printf("\nInitial values \n");
  printf("\nAddress of one = %x two = %x ptr1 = %x ptr2 = %x\n", 
            &one, &two, &ptr1, &ptr2);
  printf("Value of one = %d two = %d ptr1 = %x ptr2 = %x\n", 
            one, two, ptr1, ptr2);
  printf("Value of *ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2);

  swap(&one, &two);
  printf("\nAfter swapping one and two\n");
  printf("\nAddress of one = %x two = %x ptr1 = %x ptr2 = %x\n", 
            &one, &two, &ptr1, &ptr2);
  printf("Value of one = %d two = %d ptr1 = %x ptr2 = %x\n", 
            one, two, ptr1, ptr2);
  printf("Value of *ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2);

  swap(ptr1, ptr2);
  printf("\nAfter swapping ptr1 and ptr2\n");
  printf("\nAddress of one = %x two = %x ptr1 = %x ptr2 = %x\n", 
            &one, &two, &ptr1, &ptr2);
  printf("Value of one = %d two = %d ptr1 = %x ptr2 = %x\n", 
            one, two, ptr1, ptr2);
  printf("Value of *ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2);

  swap_ptr(&ptr1, &ptr2);
  printf("\nAfter swapping &ptr1 and &ptr2\n");
  printf("\nAddress of one = %x two = %x ptr1 = %x ptr2 = %x\n", 
            &one, &two, &ptr1, &ptr2);
  printf("Value of one = %d two = %d ptr1 = %x ptr2 = %x\n", 
            one, two, ptr1, ptr2);
  printf("Value of *ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2);

  return (0);
}


void swap(int *one, int *two)
{
  int temp = *one;

   *one = *two;
   *two = temp;
}



void swap_ptr(int **one, int **two)
{
  int *temp = *one;

   *one = *two;
   *two = temp;
}