Here's something I don't understand. Maybe someone can shed some light on it.
I know that the standard method for data manipulation is passing a reference to a function and changing the data in the function. Like this:
#include <stdio.h>
#include <stdlib.h>
void function2( float *param) {
printf("I've received value %f\n", *param);
(*param) ;
}
int main(void) {
float variable = 111;
function2(&variable);
printf("variable %f\n", variable);
return 0;
}
When calling function2 with (&variable) I expect that the function can change the data. So far no questions.
But why does this work as well?
#include <stdio.h>
#include <stdlib.h>
void function2( float ¶m) {
printf("I've received value %f\n", param);
(param) ;
}
int main(void) {
float variable = 111;
function2(variable);
printf("variable %f\n", variable);
return 0;
}
In my understanding, when calling function2(variable) a copy of the value of "variable" is passed to the function. But nevertheless the value of "variable" has changed after the function call.
When reading a code like this I would never expect the data of "variable" to change, no matter what happens inside the function.
CodePudding user response:
This declaration of the parameter
void function2( float ¶m) {
is incorrect. In C there is no reference. Such a function declaration will be valid in C , where references exist.
In C when the function is called like
function2(variable);
neither copy of the variable is created. The function refers to the original variable declared in main.
CodePudding user response:
Variation 1 expects a pointer as argument. Hence you need to explicitly pass in &variable
.
Variation 2 expects a reference as argument. They're kinda like a pointer in that they still point to the original place in memory, but also a bit different in that they're, type wise, a bit more than a mere address in memory. Like, you can't do funky pointer arithmetic with a reference if I recall correctly.
As you can see, if a function expects a reference as argument, the compiler will know what to do without you having to explicitly create a reference.
Also, as you can see, this has the potential for nasty surprises if someone calls your function and didn't expect it to change the original data. That's why in clean code you don't introduce unexpected side effects; if a function messes with input that should be the only thing it does and it should be quite explicit.
To finally get a variation that does not change the original, do
void function2( float param)
in the function declaration. No *
and no &
. In this case the function doesn't get a pointer or a reference but simply a float value, which will now be a copy of the input argument and thus won't change your variable.
CodePudding user response:
The code you posted does not compile with a C compiler as it is not valid C code. There are no reference data types in C unlike C . A variable or parameter name can not begin with an ampersand. Please use a C compiler if you want to write in C. The most popular one would be The GNU C Compiler (GCC) https://gcc.gnu.org/ and a ligher alternative would be the TinyCC, both available in most common distributions repositories.
CodePudding user response:
Your first code example uses a pointer to (i.e. an address of) the parameter. You call this a 'reference', which isn't wrong theoretically speaking. But because in C a reference is something different (see below), it's rather uncommon to use this term for a pointer.
Your second code example does use a reference. But this is not C. References are a C feature. This means that the compiler you use accepts this (and is probably a C compiler).
C is a superset of C, that's why a program can appear to be C but uses C features.