c++ - Declaration of the function pointer -


what declare following definition:

void (*bar)(a*){ }; //1 

my first thought declare , define function pointer , function pointer point to. it's wrong, because call bar() leads segmentation fault:

#include <iostream> #include <vector> #include <memory>  struct a{ };  void foo(a*){ std:cout << "foo" << std::endl; }  void (*bar)(){ };  int main(){     bar(); } 

moreover, can't imbed statement "definition":

void (*bar)(a*){ std::cout << "foo" << std::endl }; 

yeilds compile-time error.

so, declaration //1 mean?

this statement:

void (*bar)(a*){ }; 

declares variable named bar of type void(*)(a*), ie "pointer function taking pointer , returning void", , zero-initializes it. thus, it's equivalent this:

void (*bar)(a*) = nullptr; 

obviously, when calling bar, segfault should no surprise.

it's not possible declare function , pointer function in single declaration.


Comments