I have following c code:
#define ASSERT_ACTIVE(active) do { if (!active) return errno = 6, -1; } while (0);
#define errno (*_errno())
int someCrazyFunc (bool active) {
ASSERT_INACTIVE (active);
...
}
As far as I know a #define will simply place replacement text in place of the specified identifier.
I like to know:
- What does
return errno = 6, -1;means? is that returns two values in one return statement? - What is the meaning of replacement code
(*_errno()) = 6
There isn’t a second value – a
returnstatement returns exactly one value. In the statement:The return value is the result of the expression
errno = 6, -1. This is an expression using the comma operator – it is parsed as(errno = 6), -1, which evaluates to-1and assigns6toerrnoas a side-effect. So this means that it’s equivalent to the two statements:Assuming that
_errno()is a function returning a pointer – for example it has a return type ofint *– then the expression(*_errno()) = 6assigns the value6to the object pointed to by the return value of the function. It would be equivalent to code similar to:errnois often defined like this in order to give each thread in a multi-threaded implementation its ownerrno. The function_errno()in this case would return a pointer to the current thread’serrnovariable.