在不使用单独的 typedef 的情况下声明函数指针数组的语法是什么? [英] What's the syntax for declaring an array of function pointers without using a separate typedef?

查看:29
本文介绍了在不使用单独的 typedef 的情况下声明函数指针数组的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以像这样创建函数指针数组:

Arrays of function pointers can be created like so:

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

在不使用typedef的情况下创建函数指针数组的语法是什么?

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

推荐答案

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 

所以你的答案是

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

但很自然,这是一个不好的做法,只需使用 typedefs :)

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

额外:想知道如何声明一个由 3 个指针组成的数组,该数组指向采用 int 的函数并返回一个指向一个由 4 个指针组成的数组的指针,该数组指向采用 double 并返回 char 的函数?(这有多酷,嗯?:))

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

:))

这篇关于在不使用单独的 typedef 的情况下声明函数指针数组的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆