Puzzle P20

swap with pointers

The Fix to Swap

The swap() function in the previous puzzle did not work. Look at the following code (repeated from puzzle 19). Your job is to fix it so that swap() works correctly.

#include <stdio.h>

void swap( int x, int y )  
{
  int temp;

  printf("  x=%d  y=%d\n", x, y ) ;
  temp = x;
  x = y;
  y = temp;
  printf("  x=%d  y=%d\n", x, y ) ;
   
}

void main ( void )
{
  int a = 44, b = 77 ;

  printf("a=%d  b=%d\n", a, b ) ;
  swap( a, b ) ;
  printf("a=%d  b=%d\n", a, b ) ;
}

Hint: you can do this by inserting sixteen characters.



Previous Page        Answer         Next Page         Home