Using the unary & operator operator for the return value of a function

I wanted to use the unary '&' operator immediately after the function to work with the return value of the function. However, I get a compile-time error (I use gcc from MinGW)

test.c: In the "main" function:

test.c: 8: 12: error: lvalue required as unary '&' Operand

I made the code to simplify my question:

int function(); void function2(int *param); main() { function2(&function1()); } int function1() { return 10; } void function2(int *param) { return; } 

This code generates the same compile-time error.

Question: How can I use the '&' operator only from function2 "()", without another code elsewhere?

+6
source share
5 answers

What you want can be achieved on C99 and later via:

 function2((int[]){function1()}); 

i.e. creating a composite literal containing the return value of the function.

+11
source

You can not. The return value of a function is a value, but not an object. It has no designated memory location, no address, and therefore you cannot take a pointer to it. Instead, you can write:

 int a = function1(); function2(&a); 
+10
source

The problem is that & accepts the address of variables. The return value of the function is not necessarily stored in memory. In other words, this is not an lvalue (you cannot put it to the left = ). Therefore, it makes no sense to request your address (because it does not exist).

What you need to do is the following:

 int result = function1(); function2(&result); 

The C11 standard defines an lvalue in 6.3.2.1 as:

The lvalue value is an expression (with an object type other than void) that potentially denotes an object; (footnote: the name โ€œlvalueโ€ comes originally from the assignment expression E1 = E2, in which the operand E1 must be (mutable) lvalue on the left. This is perhaps better considered representing the object. '' What is sometimes called โ€œrvalueโ€ is described in this International Standard as "meaning of expression"

In the same standard, paragraph 6.5.3.2 states:

The operand of a unary and operator must be either a function designator, or the result of [], or a unary * operator, or the value l, which designates an object that is not a bit field, and is not declared using the register storage class specifier.

+3
source

You cannot get the address of the value returned by the function.

What if this value is passed back to the caller through the CPU register?

The only way to force function2 () to use the result of function1 () is to use an intermediate variable that has an address in memory.

 main() { int a; a = function1(); function2(&a); } 
+2
source

You cannot do this directly. The return value of the function can be stored on the stack (and will soon be overwritten in another function call), or maybe in registers, etc. (Depending on the calling agreement).

You probably don't want to go directly to any of these places, and that doesn't even make much sense (well, maybe if you really don't know what you are doing).

The return value of the function you are working with is the rvalue value (well ... just the value), and the & operator, as you saw in the compiler output, has the value lvalue (which you can assign). Ergo, just store the return value in a variable (local variable inside your "main ()", then get the address).

 int main() { int x = function1(); function2(&x); } 
+1
source

Source: https://habr.com/ru/post/920043/


All Articles