Created
January 3, 2023 13:41
-
-
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
This file contains 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
#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