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.

  1. Explain

    Explain why cyclic swap through pointers uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Apply the rotation twice to the original values.

Cyclic Swap Through Pointers: follow the data

  1. Given data

    Three classroom counters must move cyclically without losing the value stored in the last counter.

  2. 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.

  3. Observed result

    3 1 2

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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

  1. Start with the sample

    The saved value is three.

  2. Apply the rule

    The three caller objects become three, one and two after the stores complete.

  3. Check the result

    3 1 2

    3 1 2
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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.

What is passed to rotate?
Choose one answer. You can try again.

Answer and explanation

Copies of addresses to the caller’s objects.

Correct. The stores follow those copied pointers to the existing integers.

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.

    Next lesson: C Program to Find All Roots of a Quadratic Equation →