Implement swap_ints so the caller's two integer variables exchange values. It must do nothing when either pointer is null.
Requirements
Mutate the caller's objects
Handle null pointers
Use one temporary value
Example
Input
3, 9
Expected output
9, 3
Show a hint
Write down the input contract and the failure cases before coding.
local runtime
RUNTIME OUTPUT
Use your C runtime locally, then compare with solution.c.
Use your local runtime, then review the reference solution
LEARN FROM THE SOLUTION
Why the solution works.
The pointers are addresses, while *left and *right are the caller-owned integers. A temporary preserves the first value while the assignments happen. The alias check is not required for correctness here, but documents that no work is needed when both parameters name the same object.
What this exercise teaches
Dereferencing pointers
Mutation through addresses
Aliasing checks
A PRACTICAL PLAN
Work through Swap two integers through pointers with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Swap two integers through pointers” teach?
This beginner C exercise focuses on Dereferencing pointers, Mutation through addresses, Aliasing checks. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Write and compile the starter in your local C environment, exercise the example and edge cases, then compare your approach with solution.c.
When should I open the reference solution?
Attempt Swap two integers through pointers first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.