Jaka jest składnia deklaracji tablicy wskaźników funkcji bez użycia oddzielnego typedef?
Tablice wskaźników funkcji można tworzyć w następujący sposób:
typedef void(*FunctionPointer)();
FunctionPointer functionPointers[] = {/* Stuff here */};
Jaka jest składnia tworzenia tablicy wskaźników funkcji bez użycia typedef
?
53
3 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
Więc Twoja odpowiedź to
void (* arr [])() = {};
Ale oczywiście jest to zła praktyka, wystarczy użyć typedefs
:)
Extra: Ciekawe jak zadeklarować tablicę 3 wskaźników do funkcji biorących int i zwracających wskaźnik do tablicy 4 wskaźników do funkcji biorących double i zwracających znak? (jakie to fajne, co? :))
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
:))
101
Author: Armen Tsirunyan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-15 17:13:58
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-15 17:13:58
Pamiętaj "delcaration mimics use". Więc aby użyć tej tablicy, powiedziałbyś
(*FunctionPointers[0])();
Zgadza się? Dlatego aby go zadeklarować, używasz tego samego:
void (*FunctionPointers[])() = { ... };
15
Author: Logan Capaldo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-23 15:40:34
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-23 15:40:34
Użyj tego:
void (*FunctionPointers[])() = { };
Działa jak Wszystko inne, umieszczasz [] po nazwie.
4
Author: Erik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-23 15:39:21
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-23 15:39:21