Consider the following C99 function:
void port_pin_set(const bool value, const uint8_t pin_mask)
{
if (value) {
PORT |= pin_mask;
} else {
PORT &= ~pin_mask;
}
}
With PORT being a define, for example:
#define PORT (P1OUT)
Is there a way to redefine PORT so that:
- it is accepted as an lvalue,
- the function does not do anything.
What I want to do is to keep the function source as is, while compiling it to do nothing.
Edit: I am aware that using such an lvalue might not be the best solution. I am not looking for the best solution for this specific problem, I am interested in the language itself. This is a theoretical question, not a pragmatic one.
C99 has compound literals that can be used for such a task. Their syntax is a cast followed by an initializer:
should do it in your case, only that you might get some warnings about unused values or so.
Any decent compiler will optimize assignments to such compound literal out.