Decryption (* (void (*) ()) 0) ()

They said that this expression is valid in C, and that it means calling a function:

(*(void(*)())0)(); 

Can someone clearly explain what this expression means?

I tried to compile this and was surprised that this did not result in an error.

+6
c syntax function-pointers
source share
5 answers

Step by step:

  void(*)() // a pointer-to-function type, taking unspecified parameters // and returning nothing. (void(*)())0 // a null pointer of that pointer-to-function type (*(void(*)())0) // dereference that pointer (*(void(*)())0)(); // and call it with no parameters 

There is undefined behavior in the code, it is probably crashing with some illegal access / segfault.

+18
source share

You create a pointer to a function and then call it. I would not call it a hidden function, but undefined.

Basically you do this, but instead address instead:

 void test() { } void(*pfn)() = test; (*pfn)(); 
+6
source share

This is a pointer to a function in NULL .

void(*)() is the definition of a pointer to a function that does not contain arguments that return nothing; you can name it:

 typedef void(*my_func)(); 

then in your example you have a cast:

(my_func)0 gives a pointer to the my_func function, that is, a function that takes nothing and returns nothing.

Then you cast it with an asterisk (optional, afaik), and then you call it.

So, you call a function that takes no arguments and returns nothing that happens at the zero address.

This behavior is (usually) undefined and instantly crashing on many platforms. (This is not undefined behavior if you put the function at address zero, at least I would not have thought what it was.)

+3
source share

Separate it in parentheses.

The last () means a function without parameters.

The line (void(*)()) means a function that returns void.

The last bit, (* at the beginning and 0) tells the compiler that the address of the called function lies in the location of pointer 0.

So basically you call that the trait is at address 0 with no parameters. This is usually not very safe. :)

+2
source share

in an embedded environment, it may be a way to invoke a reset system.

0
source share

All Articles