If C does not support passing a variable by reference, why does this work?
#include <stdio.h> void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf("i = %d\n", i); return 0; }
$ gcc -std=c99 test.c $ a.exe i = 21
Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.
Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference.
"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value). So, in your example, it would pass an integer object with the value of 9.
Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.
Because you're passing the value of the pointer to the method and then dereferencing it to get the integer that is pointed to.
That is not pass-by-reference, that is pass-by-value as others stated.
The C language is pass-by-value without exception. Passing a pointer as a parameter does not mean pass-by-reference.
The rule is the following:
A function is not able to change the actual parameters value.
Let's try to see the differences between scalar and pointer parameters of a function.
This short program shows pass-by-value using a scalar variable. param
is called the formal parameter and variable
at function invocation is called actual parameter. Note incrementing param
in the function does not change variable
.
#include <stdio.h> void function(int param) { printf("I've received value %d\n", param); param++; } int main(void) { int variable = 111; function(variable); printf("variable %d\m", variable); return 0; }
The result is
I've received value 111 variable=111
We change the piece of code slightly. param
is a pointer now.
#include <stdio.h> void function2(int *param) { printf("I've received value %d\n", *param); (*param)++; } int main(void) { int variable = 111; function2(&variable); printf("variable %d\n", variable); return 0; }
The result is
I've received value 111 variable=112
That makes you believe that the parameter was passed by reference. It was not. It was passed by value, the param value being an address. The int type value was incremented, and that is the side effect that make us think that it was a pass-by-reference function call.
How can we show/prove that fact? Well, maybe we can try the first example of Scalar variables, but instead of scalar we use addresses (pointers). Let's see if that can help.
#include <stdio.h> void function2(int *param) { printf("param's address %d\n", param); param = NULL; } int main(void) { int variable = 111; int *ptr = &variable; function2(ptr); printf("ptr's address %d\n", ptr); return 0; }
The result will be that the two addresses are equal (don't worry about the exact value).
Example result:
param's address -1846583468 ptr's address -1846583468
In my opinion this proves clearly that pointers are passed-by-value. Otherwise ptr
would be NULL
after function invocation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With