Skip to content

Instantly share code, notes, and snippets.

@dbc2201
Created November 29, 2018 11:23
Show Gist options
  • Save dbc2201/a2d3ff8a7747d9dc64ad93e0f4ad526e to your computer and use it in GitHub Desktop.
Save dbc2201/a2d3ff8a7747d9dc64ad93e0f4ad526e to your computer and use it in GitHub Desktop.
Swap two variables using pointers. Call By Reference
#include <stdio.h>
void swap(int *a1, int *b1);
int main()
{
int a = 4, b = 5;
printf("a = %d b = %d\n", a, b);
swap(&a, &b);
printf("a = %d b = %d\n", a, b);
return 0;
}
void swap(int *a1, int *b1)
{
*a1 = *a1 + *b1;
*b1 = *a1 - *b1;
*a1 = *a1 - *b1;
printf("%d %d\n", *a1, *b1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment