n个指针的数组返回函数指针的函数 [英] array of N pointers to functions returning pointers to functions

查看:174
本文介绍了n个指针的数组返回函数指针的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是要求我在接受采访时!
我真的糊涂了。

This was asked to me in an interview! i really got confused


  • 如何,我宣布N的数组
    指针函数返回
    指针函数返回
    指针字

可能有人请帮助?

推荐答案

类型定义是wusses。下面是搞清楚毛茸茸声明一个简单的,机械的方法:

Typedefs are for wusses. Here's a straightforward, mechanical method for figuring out hairy declarations:

          a                 -- a
          a[N]              -- is an N-element array
         *a[N]              -- of pointers
        (*a[N])()           -- to functions
       *(*a[N])()           -- returning pointers
      (*(*a[N])())()        -- to functions
     *(*(*a[N])())()        -- returning pointers
char *(*(*a[N])())()        -- to char.  

那么,答案是的char *附近(*(* A [N])())(); 。我说:在附近,因为它从来没有规定的职能采取什么样的参数。

So, the answer is in the neighborhood of char *(*(*a[N])())();. I say "in the neighborhood" since it's never specified what arguments the functions take.

这是一个讨厌的面试问题(类型这个丑陋是真正难得的IME),但它确实给面试官你是如何很好地理解说明符的概念。如果不是这样,他们是无聊,只是想看看他们是否可以让你的大脑sieze。

It's an obnoxious interview question (types this ugly are truly rare IME), but it does give the interviewer an idea of how well you understand declarators. Either that or they were bored and just wanted to see if they could make your brain sieze.

修改

大多数其他人建议使用的typedef。我建议使用一个typedef唯一的一次是,如果类型是为了成为真正不透明的(即不是由程序员直接操作,但传递给一个API,有点像文件类型)。否则,如果程序员是为了直接操纵该类型的对象,那么IME最好是都在声明中提供的资料,丑陋的,因为它可能。例如,像

Most everyone else recommends using typedefs. The only time I recommend using a typedef is if the type is intended to be truly opaque (i.e., not manipulated directly by the programmer, but passed to an API, sort of like the FILE type). Otherwise, if the programmer is meant to manipulate objects of that type directly, then IME it's better to have all that information available in the declaration, ugly as it may be. For example, something like

 NameFuncPickerPointer a[N];

使我对如何实际使用 A [I] 的信息。我不知道, A [I] 是可调用的,或者它返回什么,或者应该采取什么样的参数(如果有的话),或别的东西也不多。我必须去寻找的typedef

gives me no information on how to actually use a[i]. I don't know that a[i] is callable, or what it returns, or what arguments it should take (if any), or much of anything else. I have to go looking for the typedef

typedef char *NameFunc();
typedef NameFunc *NameFuncPicker();
typedef NameFuncPicker *NameFuncPickerPointer;

和从谜题如何写前pression实际调用的功能之一。而使用了赤裸,非typedef定义声明,我立即知道来电的结构

and from that puzzle out how to write the expression that actually calls one of the functions. Whereas using the "naked", non-typedef'd declaration, I know immediately that the structure of the call is

char *theName = (*(*a[i])())();

这篇关于n个指针的数组返回函数指针的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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