What's the syntax for declaring an array of function pointers without using a separate typedef?

C++ArraysSyntaxFunction PointersTypedef

C++ Problem Overview


Arrays of function pointers can be created like so:

typedef void(*FunctionPointer)();
FunctionPointer functionPointers[] = {/* Stuff here */};

What is the syntax for creating a function pointer array without using the typedef?

C++ Solutions


Solution 1 - C++

arr    //arr 
arr [] //is an array (so index it)
* arr [] //of pointers (so dereference them)
(* arr [])() //to functions taking nothing (so call them with ())
void (* arr [])() //returning void 

so your answer is

void (* arr [])() = {};

But naturally, this is a bad practice, just use typedefs :)

Extra: Wonder how to declare an array of 3 pointers to functions taking int and returning a pointer to an array of 4 pointers to functions taking double and returning char? (how cool is that, huh? :))

arr //arr
arr [3] //is an array of 3 (index it)
* arr [3] //pointers
(* arr [3])(int) //to functions taking int (call it) and
*(* arr [3])(int) //returning a pointer (dereference it)
(*(* arr [3])(int))[4] //to an array of 4
*(*(* arr [3])(int))[4] //pointers
(*(*(* arr [3])(int))[4])(double) //to functions taking double and
char  (*(*(* arr [3])(int))[4])(double) //returning char

:))

Solution 2 - C++

Remember "delcaration mimics use". So to use said array you'd say

 (*FunctionPointers[0])();

Correct? Therefore to declare it, you use the same:

 void (*FunctionPointers[])() = { ... };

Solution 3 - C++

Use this:

void (*FunctionPointers[])() = { };

Works like everything else, you place [] after the name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxpmView Question on Stackoverflow
Solution 1 - C++Armen TsirunyanView Answer on Stackoverflow
Solution 2 - C++Logan CapaldoView Answer on Stackoverflow
Solution 3 - C++ErikView Answer on Stackoverflow