chuy
chuy

Reputation: 15

Address operator "&" with function returns

This:

bit_is_set(optos(),opt)

expanding macro bit_is_set:

((*(volatile uint8_t *)(((uint16_t) &(optos())))) & (1 << (opt)))

is not working, with error message: lvalue required as unary '&' operand.

But this:

uint8_t a=optos();
bit_is_set(a,opt)

works fine.

Why?

How do I use the address operator "&" with function returns?

Upvotes: 0

Views: 129

Answers (1)

Marco A.
Marco A.

Reputation: 43662

For the same reason why this won't work:

uint8_t optos() {
    return 4;
}

int main(void) {
    uint8_t* addr = &optos();
    return 0;
}

error: lvalue required as unary ‘&’ operand

The & operand requires an lvalue to return an address. A temporary rvalue (in your case the value returned from optos()) can't have its address taken and needs to be bound to a lvalue first.

Upvotes: 2

Related Questions