English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, we use by reference call to cyclically swap the three numbers entered by the user.
To understand this example, you should understand the followingC Programming LanguageTopic:
#include <stdio.h> void cyclicSwap(int *a, int *b, int *c); int main() { int a, b, c; printf("Enter a, b, and c: "); scanf("%d %d %d", &a, &b, &c); printf("The values before swapping:\n"); printf("a = %d b = %d c = %d\n", a, b, c); cyclicSwap(&a, &b, &c); printf("The values after swapping:\n"); printf("a = %d b = %d c = %d", a, b, c); return 0; } //Circular swap void cyclicSwap(int *n1, int *n2, int *n3) { int temp; temp = *n2; *n2 = *n1; *n1 = *n3; *n3 = temp; }
Output result
Enter a, b, and c separately: 1 2 3 The values before swapping: a = 1 b = 2 c = 3 The values after swapping: a = 3 b = 1 c = 2
Here, the three numbers entered by the user are stored in variables a, b, and c. The addresses of these numbers will be passed to the cyclicSwap() function.
cyclicSwap(&a, &b, &c);
In the function definition of cyclicSwap(), we have assigned these addresses to pointers.
cyclicSwap(int *n1, int *n2, int *n3) { ... }
When n in cyclicSwap()1and n2and n3When the values of a, b, and c in main() change.
Note:The cyclicSwap() function does not return anything.