0

Consider following code which declares a function pointer, and points it to a function.

void MyIntFunction( int x ){
    std::cout << x << '\n';
}

void ( *funcPtr )( int ) = MyIntFunction;
void ( *funcRef )( int ) = &MyIntFunction;


(*funcPtr)( 2 );
(*funcRef)( 2 );

This code runs fine in my Xcode, and the question is, when assigning the function pointer, what is the difference between MyIntFunction and &MyIntFunction

Curtis2
  • 106
  • 7

1 Answers1

3

Formally MyIntFunction is a function type, and &MyIntFunction is a pointer-to-function type. However, function type decays to the pointer-to-function type in almost all contexts, so there is no real difference between using MyIntFunction and &MyIntFunction in this context.

Please note, there is no reference anywhere in the posted code. Both funcPtr and funcRef are pointers-to-function.

SergeyA
  • 61,605
  • 5
  • 78
  • 137