Created
June 15, 2015 03:40
-
-
Save tudorconstantin/7bc4f127c20ef7ebcb53 to your computer and use it in GitHub Desktop.
exchange 2 integers using pointers in c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using pointers to exchange values | |
#include <stdio.h> | |
void exchange_values( int * const pint1, int * const pint2) | |
{ | |
int temp; | |
temp = *pint1; | |
*pint1 = *pint2; | |
*pint2 = temp; | |
} | |
int main(void) | |
{ | |
void exchange_values( int * const pint1, int * const pint2); | |
int i1 = -5; int i2 = 66, *p1 = &i1, *p2 = &i2; | |
printf(" i1 = %d, i2 = %d\n", i1, i2); | |
exchange_values(p1, p2); | |
printf(" i1 = %d, i2 = %d\n", i1, i2); | |
exchange_values(&i1, &i2); | |
printf(" i1 = %d, i2 = %d\n", &i1, &i2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment