I just came across this in uC-OS3 source code. What does it do?
I didn't include the real source for for copyright reasons. Here's a similar one:
static void x(int y)
{
(void)&y;
}
I really can't think of what it does, besides to mute the unused variable warning from the compiler.
Any suggestions?
As written that code fragment does nothing - it takes the address of the parameter y but does nothing with it. The cast to void will just prevent the compiler issuing a "value not used" warning.
It passes the address of a function as uint32_t.
Then it converts back the uint32_t into a function_pointer
If this is supposed to be C then you can not do this. Pointers to data and pointers to functions are not compatible. No use of casts or other tricks can get around this.
Largely yes - in this case &y is the address of a parameter, in most architectures it will be on the stack so using it as a function address will almost certainly not produce a useful result.
That said maybe one could contrive for someting useful to happen depending on the exact architecture by taking the address of a variable and using it as a function pointer but that is really horrible code with no role outside some sort of obfusicated C contest.
But C generally makes no distinction between a data address and a function address - anything which can be cast to a function pointer type can be used to call a function.
It would be perfectly legal C to say
1
(void (*)())(&y)();
[1] I think - bit rusty on such arcanery.
NB
https://cdecl.org/ is *very* useful for figuring out the syntax for complex declarations and casts.