Call by Value and Call by Reference
C passes copies of arguments; to let a function modify a variable, pass its address with a pointer.
Easy
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int x = 1, y = 2;
swap(&x, &y); // x = 2, y = 1
C is strictly call by value: the function receives copies. Passing a pointer copies the address, which lets the function reach the original variable — this is how C emulates call by reference. Arrays are passed as pointers to their first element.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.