What does C expression ((void(*)(void))0)(); mean?

This is a function expecting no arguments and returning no value:

void f(void)

This is a pointer to a function expecting no arguments and returning no value:

void (*p)(void)

This is the type of that pointer:

void (*)(void) /* just remove the p! */

This is that type in parentheses:

(void (*)(void))

This is a cast to that type (the type in parentheses, followed by a value):

(void (*)(void))0

Still with me? so far we have the integer value 0 cast to a pointer-to-function-that-takes-no-arguments-and-returns-nothing.

The cast is an expression with pointer-to-function type. When you have one of those you can call it like this:

(your expression here)(arguments to the function)

The first set of parentheses are just for precedence, and sometimes might not be needed (but this time they are). The end result:

((void (*)(void))0)(/* no args */);

Takes the value 0, casts it to pointer-to-function-expecting-no-arguments-and-returning-nothing, and calls it, supply no arguments.


The syntax to cast address to a function pointer and then call it would look like this:

((void (*)(void))address)();

It might be clearer to do something like this though:

void (*fptr)(void) = (void (*)(void))address;
fptr();

Said that ((void(*)(void))0)(); instruction is used to jump to 0 in firmwares usually. It is a bit improper because it actually calls in 0 instead of jumping to 0, but practically it won't make any difference (a fw hot reboot will be performed)


This treats NULL as a function pointer and executes it, it should raise a sigbus or similar on most systems.

void(*)(void)   <- type, function pointer taking no arguments and returning no value
(void(*)(void)) <- cast to above type
((...)0)        <- cast NULL/0 to said type
((...)0)()      <- execute the cast value as a function

Tags:

C