6

i came across this syntax and i am not able to know how to start understanding this.

How to start decoding of such piece of c programming code.

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

i have tried to compile this code and it compile without any warning or error. SO it seems valid syntax of c programming.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222

1 Answers1

5

Break it as follows:

  • (void(*)()) represents a cast of 0. Here its is a pointer to a function with return type void and can have any number of arguments.

       (  void  (*)  ( )  )
           ^     ^    ^
           |     |    |
           |     |    |
           |     |    |
           |     |    |
           +     |    +
    Return type  |    Function
                 |
              Pointer
    
  • *(void(*)())0 is dereferencing the address 0x00000000. I think its a function address there.

  • (*(void(*)())0)(); call the function.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Aftrer reading 5 times your answer now it seems i could understood it.. Please add some more text to understand it faster way.. – Jeegar Patel Dec 20 '15 at 06:23
  • at address 0 what function would be there? if no function then segmentation fault or what ever written at that memory it will execute that right? – Jeegar Patel Dec 20 '15 at 06:25
  • @JeegarPatel; Yes. There may be code related to boot. – haccks Dec 20 '15 at 06:26
  • 2
    @JeegarPatel If you're programming at the hardware level then whatever function is at address 0 is whatever happens to be there -- or no function at all. Some hardware might not even allow you to reference that address. – Carey Gregory Dec 20 '15 at 06:28
  • @haccks So here in your step1,, Missing function pointer name and no arguments makes this code to confusing here. – Jeegar Patel Dec 20 '15 at 06:34
  • @JeegarPatel; Pointer is confusing to me too :) – haccks Dec 20 '15 at 06:36
  • Casting a null pointer constant to a pointer produces a *null pointer*. This might not represent address zero. So "dereferencing whatever be at 0" is not a good description. – M.M Dec 20 '15 at 06:47
  • "represents a cast to 0" -> "represents a cast *of* `0`" – M.M Dec 20 '15 at 06:47
  • @M.M; Or it might [represent](http://stackoverflow.com/questions/15089896/null-function-pointers?lq=1#comment21242366_15090036). – haccks Dec 20 '15 at 06:55