Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using unary & operator on function return value

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?

like image 665
pampeho Avatar asked Jul 09 '12 15:07

pampeho


3 Answers

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.

like image 127
R.. GitHub STOP HELPING ICE Avatar answered Oct 12 '22 18:10

R.. GitHub STOP HELPING ICE


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);
like image 12
Steve Jessop Avatar answered Oct 12 '22 16:10

Steve Jessop


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.

like image 4
Shahbaz Avatar answered Oct 12 '22 16:10

Shahbaz