Home c How to create an array of function pointers in C / C++

How to create an array of function pointers in C / C++

Author

Date

Category

or

void (** pfn_MyFuncType) (char * str, int len);

Answer 1, authority 100%

It is necessary to declare a new type – a pointer to a function. This is done by simply declaring a function pointer. Next, you need to declare an array of the type of a pointer to a function. For example:

typedef void (* pfn_MyFuncType) (char * str, int len);
pfn_MyFuncType * myFuncTypeArray;
// or
pfn_MyFuncType myFuncTypeArray [10];

Answer 2

C++ 11 style & lt;

# include & lt; vector & gt;
#include & lt; iostream & gt;
#include & lt; functional & gt;
void foo (int i) {
  std :: cout & lt; & lt; i & lt; & lt; '\ n';
}
int main () {
  std :: vector & lt; std :: function & lt; void (int) & gt; & gt; vfunc;
  vfunc.push_back (foo);
  vfunc [0] (1);
  return 0;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions