C PROGRAMMING PROGRAM • FUNCTIONS, PREPROCESSOR & COMMAND LINE
Simulate Call by Reference Using Pointers
Learn how to simulate call by reference using pointers using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
After call: 20 10
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
void swap_values(int *first, int *second)
{
int temporary = *first;
*first = *second;
*second = temporary;
}
int main(void)
{
int first = 10, second = 20;
swap_values(&first, &second);
printf("After call: %d %d\n", first, second);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
After call: 20 10
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Pass variable addresses so the function can modify the original values through dereferencing.
- Display the computed result.
Pass variable addresses so the function can modify the original values through dereferencing.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
