0

There is this code:

void a() { }

int main(){
   a();
   (****&a)();
   return 0;
}

How does it happen that statement (****&a)(); has the same effect as a();?

scdmb
  • 15,091
  • 21
  • 85
  • 128
  • 1
    `(**********************************************************&a)();` works too. – masoud May 18 '13 at 15:46
  • Oh guys, really? With [12 minutes worth of difference?](http://stackoverflow.com/questions/16625902/function-variable-instead-of-pointer-to-function) –  May 18 '13 at 15:46

1 Answers1

7

It's all because of function-to-pointer conversion (§4.3):

An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.

&a first gives you a pointer to a. Then you dereference it with * giving you the function itself. You then attempt to dereference that function, but since you can't, it undergoes function-to-pointer conversion to get the pointer again. You dereference that pointer with *, and so on.

In the end (****&a) denotes the function a and you call it, since you can apply () to a function without it undergoing function-to-pointer conversion.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324