C Cyclic Swap Using Pointer Parameters
C Cyclic Swap Using Pointer Parameters
Three classroom counters must move cyclically without losing the value stored in the last counter.
Pass addresses by value so a helper can store through pointers into caller-owned objects. This gives a reference-like effect without changing C’s pass-by-value rule.
Before this exercise, review functions and copied parameters and recursion and stopping cases. The worked program below applies those ideas to this specific problem.
Explain
Explain why cyclic swap through pointers uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Apply the rotation twice to the original values.
Cyclic Swap Through Pointers: follow the data
Given data
Three classroom counters must move cyclically without losing the value stored in the last counter.
Rule
Pass addresses by value so a helper can store through pointers into caller-owned objects. This gives a reference-like effect without changing C’s pass-by-value rule.
Observed result
3 1 2
Understand the operation
The pointer arguments identify the caller’s three distinct live int objects. Save the third value, move second to third and first to second, then store the saved value in first.
The traditional call-by-reference wording survives in the existing URL. C actually copies pointer values into parameters; dereferencing those values provides access to the original objects.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
static void rotate(int *first, int *second, int *third) {
int saved = *third;
*third = *second; *second = *first; *first = saved;
}
int main(void) {
int first = 1, second = 2, third = 3;
rotate(&first, &second, &third);
printf("%d %d %d\n", first, second, third);
return 0;
}
Expected output
3 1 2
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static void rotate(int *first, int *second, int *third) {
int saved = *third;
*third = *second; *second = *first; *first = saved;
}
int main(void) {
int first = 1, second = 2, third = 3;
rotate(&first, &second, &third);
printf("%d %d %d\n", first, second, third);
return 0;
}
The saved value is three.
The three caller objects become three, one and two after the stores complete.
Follow the worked calculation
Start with the sample
The saved value is three.
Apply the rule
The three caller objects become three, one and two after the stores complete.
Check the result
3 1 2
3 1 2
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static void rotate(int *first, int *second, int *third) {
int saved = *third;
*third = *second; *second = *first; *first = saved;
}
int main(void) {
int first = 1, second = 2, third = 3;
rotate(&first, &second, &third);
printf("%d %d %d\n", first, second, third);
return 0;
}
Apply the rotation twice to the original values.
Worked answer
The second call rotates 3,1,2 into 2,3,1. Three calls would restore the initial order for three distinct objects.
C17 · complete program
#include <stdio.h>
static void rotate(int *first, int *second, int *third) {
int saved = *third;
*third = *second; *second = *first; *first = saved;
}
int main(void) {
int first = 1, second = 2, third = 3;
rotate(&first, &second, &third);
rotate(&first, &second, &third);
printf("%d %d %d\n", first, second, third);
return 0;
}
Expected output
2 3 1
Run in C compiler →Recognize the important boundaries
Require three valid, distinct int object addresses for this simple contract. Aliased arguments need a separately defined behavior.
Null or expired addresses are not valid access rights. A pointer parameter’s type does not prove its runtime lifetime.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Pass addresses by value so a helper can store through pointers into caller-owned objects. This gives a reference-like effect without changing C’s pass-by-value rule.
Continue with address and dereference operations.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.