How to declare a function pointer in C / C++

How to declare a function pointer in C / C++

 4
c++
Author: Nicolas Chabanovsky, 2010-10-20

3 answers

You must adhere to the following ad form:

typedef тип_возвращаемого_значения (*имя_указ)(список_параметров_функции);

For example:

typedef int (*pfn)(int param1, void * param2);
 8
Author: Nicolas Chabanovsky, 2010-10-20 20:01:21

Starting with c++11 can be declared via using:

using name = retType(*)(params);

Example:

void f(int, int) {}           // функция приемлимой сигнатуры 
using FP = void(*)(int, int); // объявление типа указатель на функцию
FP fp = f;                    // определение и инициализация
fp(42, 100500);               // вызов функции через указатель
 5
Author: αλεχολυτ, 2016-06-01 11:39:27
std::function

A very convenient thing ( but, of course, this does not apply to Pure C, which was in the tags ). Here you have pointers to functions, and to class methods, and to functors (!), and all sorts of mutations with bind and place-holders.

Speca

 3
Author: isnullxbh, 2016-08-18 18:43:11