Search code examples
cfunctionpointersunary-operator

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?


Solution

  • 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.