Mega Code Archive

 
Categories / C / Code Snippets
 

Swap function Exchange the values by pointers

#include <stdio.h> void swap(int *k, int *j); int main(void) { int num1, num2; num1 = 123; num2 = 166; printf("num1 = %d num2 = %d\n", num1, num2); swap(&num1, &num2); printf("num1 = %d num2 = %d\n", num1, num2); return 0; } /* Exchange the values by pointers. */ void swap(int *k, int *j) { int temp; temp = *k; *k = *j; *j = temp; }