C program for swapping of two numbers using pointers

ima.robot

Newbie
Joined
Oct 11, 2021
Messages
1
Reaction score
0
Hi,

Can anyone give me an example of a C program for swapping of two numbers using pointers.
I read https://www.scaler.com/topics/swapping-of-two-numbers-in-c/ But could not find an example, or maybe I missed out in understanding.

All help is truly appreciated.
 
Hi, it's been a while, but hopefully, this helps.

Code:
// This how we do "pass by reference" in C
void swap(int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}
int foo = 10;
int bar = 20;
swap(&foo, &bar);
 
Back
Top