No, it doesn't. It has pointers, but they're not quite the same thing.
In particular, all arguments in C are passed by value, rather than pass-by-reference being available as in C++. Of course, you can sort of simulate pass-by-reference via pointers:
void foo(int *x)
{
*x = 10;
}
...
int y = 0;
foo(&y); // Pass the pointer by value
// The value of y is now 10
For more details about the differences between pointers and references, see this SO question. (And please don't ask me, as I'm not a C or C++ programmer :)
Answer from Jon Skeet on Stack OverflowDoes C really need references?
pointers - Passing by reference in C - Stack Overflow
Which C reference book do you recommend?
pointers vs references
Are you using C++? C++ has both pointers * and references &. References are really just syntactic sugar (a convenience) for pointers.
C has no references, though.
More on reddit.comVideos
No, it doesn't. It has pointers, but they're not quite the same thing.
In particular, all arguments in C are passed by value, rather than pass-by-reference being available as in C++. Of course, you can sort of simulate pass-by-reference via pointers:
void foo(int *x)
{
*x = 10;
}
...
int y = 0;
foo(&y); // Pass the pointer by value
// The value of y is now 10
For more details about the differences between pointers and references, see this SO question. (And please don't ask me, as I'm not a C or C++ programmer :)
Conceptually, C has references, since pointers reference other objects.
Syntactically, C does not have references as C++ does.
Could the language have just used pointers instead ? I don’t really understand purpose of reference except code might be easier to write.
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.
(The above citation is actually from the book K&R)
Let's try to see the differences between scalar and pointer parameters of a function.
Scalar variables
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
Illusion of pass-by-reference
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.
Pointers - passed-by-value
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("address param is pointing to %d\n", param);
param = NULL;
}
int main(void) {
int variable = 111;
int *ptr = &variable;
function2(ptr);
printf("address ptr is pointing to %d\n", ptr);
return 0;
}
The result will be that the two addresses are equal (don't worry about the exact value).
Example result:
address param is pointing to -1846583468
address ptr is pointing to -1846583468
In my opinion this proves clearly that pointers are passed-by-value. Otherwise ptr would be NULL after function invocation.