Skip to content

Instantly share code, notes, and snippets.

@rajan-31
Created January 3, 2023 13:41
Show Gist options
  • Save rajan-31/4259cd0325ab02a31212044d22b3057b to your computer and use it in GitHub Desktop.
Save rajan-31/4259cd0325ab02a31212044d22b3057b to your computer and use it in GitHub Desktop.
In C programming language, 2 Ways to swap array elements using temp variable
#include <stdio.h>
void swap(int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
// Swap elts at index 0 and 4
swap(arr, arr+4);
println("%d\n%d", arr[0], arr[4]);
swap(&arr[0], &arr[4]);
println("%d\n%d", arr[0], arr[4]);
return 0;
}
/* Expected output
5
1
1
5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment