Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 881
Maxpm Avatar asked Feb 23 '11 15:02

Maxpm


People also ask

How do you declare a function pointer in C++?

We declare the function pointer, i.e., void (*ptr)(char*). The statement ptr=printname means that we are assigning the address of printname() function to ptr. Now, we can call the printname() function by using the statement ptr(s).

What is Typedef in function pointer?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.


1 Answers

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 

:))

like image 106
Armen Tsirunyan Avatar answered Sep 28 '22 20:09

Armen Tsirunyan