Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tudorconstantin/7bc4f127c20ef7ebcb53 to your computer and use it in GitHub Desktop.
Save tudorconstantin/7bc4f127c20ef7ebcb53 to your computer and use it in GitHub Desktop.
exchange 2 integers using pointers in c
// 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