Summary: This program swaps the values of two integers using a function that accepts pointers. The swap is performed through memory manipulation using the address-of and dereference operators.
#include <stdio.h>
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
// Input two integers
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
// Before swapping
printf("Before swap: a = %d, b = %d\n", x, y);
// Swap values using pointer
swap(&x, &y);
// After swapping
printf("After swap: a = %d, b = %d\n", x, y);
return 0;
}
Pointer Arguments:
The function swap() uses int *a and int *b to access the memory addresses of the original variables.
Dereferencing:
*a and *b are used to get the values stored at the addresses. These are then swapped using a temporary variable.
Address-of Operator:
The & operator is used in main() to pass the addresses of variables x and y to the function.
Result:
Values of x and y are successfully swapped by the function since it modifies the memory directly.