I'm not understanding a function used by my co-worker. He cannot He is using strange return blocks.
block_t *get_block(void *p)
{
char *tmp;
tmp = p;
return (p = tmp -= 16);
}
He says it is supposed to return form and the address.
Need some help to understand the return ! Thanks a lot :)
All C operators compute values (except a cast operator with void
as the target type or a function call to a function returning void
). In particular, assignment and op/assignment operations evaluate to the value assigned. Therefore, this statement:
return (p = tmp -= 16);
is equivalent to
tmp -= 16;
p = tmp;
return p;
And the latter is exactly how I would write it if that's what I wanted to do. Compacting it all into one expression makes the code harder for a human to read and understand, and conveys no offsetting advantage whatsoever.
HOWEVER, your particular case is even worse. In your case, p
and tmp
are local variables of the function, so assignments to them have no effect on the caller. There is therefore no point in assigning a new value to p
if it will not afterward be read back (from p
). This may or may not reflect a programming error. If the function's actual behavior is correct as-is, then I would write the overall function much differently:
block_t *get_block(void *p) {
return (block_t *) (((char *) p) - 16);
}