I've wanted to apply unary '&' operator just behind function to operate on function return value. However, I get a compile-time error (I use gcc from MinGW)
test.c: In function 'main':
test.c:8:12: error: lvalue required as unary '&' operand
I made a code to make my question easier to understand:
int function();
void function2(int *param);
main()
{
function2(&function1());
}
int function1()
{
return 10;
}
void function2(int *param)
{
return;
}
This code creates same compile-time error.
The question is: How I can use the '&' operator just from function2 "()", without other code elsewhere?
What you want can be achieved in C99 and later via:
function2((int[]){function1()});
i.e. making a compound literal containing the function return value.
You can't. The return value of a function is a value but not an object. It has no designated location in memory, no address, and so you can't take a pointer to it. Instead you could write:
int a = function1();
function2(&a);
The problem is that &
takes the address of variables. The return value of a function is not necessarily stored in memory. In other words, it is not an lvalue
(you cannot put it on the left side of =
). Therefore, it doesn't make sense to ask for its address (because it is non-existent).
What you should do is the following:
int result = function1();
function2(&result);
The C11 standard defines lvalue
in 6.3.2.1 as:
An lvalue is an expression (with an object type other than void) that potentially designates an object; (footnote: The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’)
In the same standard, in 6.5.3.2 it says:
The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.
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