Swap values with pointers | C

September 27th, 2021 10:43:34 AM

					
#include <stdio.h> #include <stdlib.h> int main(){ int * int_ptr; int_ptr = (int *)malloc(2*sizeof(int)); if(!int_ptr) { printf("Something went wrong while allocating memory. Exiting..."); exit(1); } printf("Enter first integer: "); scanf("%i", &int_ptr[0]); printf("Enter second integer: "); scanf("%i", &int_ptr[1]); printf("Original values: 1st = %i 2nd = %i\n",int_ptr[0], int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); int_ptr[1] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); printf("Swapped values: 1st = %i 2nd = %i\n", int_ptr[0], int_ptr[1]); free(int_ptr); exit(0); }

Featured Posts