English 中文(简体)
一个返回指向函数指针的函数指针数组N个。
原标题:array of N pointers to functions returning pointers to functions
  • 时间:2010-02-03 14:31:59
  •  标签:
  • c
  • types

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

  • How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters

请问有人可以帮忙吗?

最佳回答

typedef是给懦夫用的。这里有一个直接、机械的方法来解决复杂的声明:

          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])())(); 的邻居里。我说“在附近”是因为没有指定函数采取什么参数。

这是一个令人讨厌的面试问题(在我的经验中,这种丑陋的类型真的很少见),但它确实可以让面试官了解你理解声明符的能力。或者他们只是感到无聊,想看看他们是否能让你的大脑抽搐。

编辑 (biān jí)

大多数人都建议使用typedef。仅在类型要求真正不透明时(即,程序员不能直接操纵,而是将其传递给API,类似于FILE类型)才建议使用typedef。否则,如果程序员需要直接操纵该类型的对象,则我认为最好在声明中提供所有信息,尽管可能很丑陋。例如,像这样的一些内容

 NameFuncPickerPointer a[N];

这并没有告诉我如何实际使用a[i]。我不知道a[i]是否可调用,它返回什么,它应该接受什么参数(如果有),或者其他的任何事情。我必须去寻找typedef。

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

从中推导出如何编写实际调用其中一个函数的表达式。而使用“裸”,非typedef d声明,我立即知道调用的结构是什么。

char *theName = (*(*a[i])())();
问题回答
typedef char* (* tCharRetFunc)();
typedef tCharRetFunc (* tFuncRetCharFunc)();

tFuncRetCharFunc arr[N];

将大问题分解成小部分:

/* char_func_ptr is pointer to function returning pointer to char */
typedef char* (*char_func_ptr)();

/* func_func_ptr is a pointer to function returning above type */
typedef char_func_ptr (*func_func_ptr)();

/* the_array is array of desired function pointers */
func_func_ptr the_array[42];

一个返回指向函数指针的指针函数指针数组,返回字符。

int (*(*arr_fp[n])(void))(void)

这是你正在寻找的吗?

typedef char* charptr;
typedef charptr (*innerfun)();
typedef innerfun (*outerfun)();

const size_t N = 10;
outerfun my_outerfun_array[N];

我希望我的回答是正确的,对我来说,这似乎是一个奇怪的问题,特别是在面试中 :(

使用Christopher告诉你的typedef作为声明这种东西的惟一人道的方式。如果没有typedef,它将变成:

char *(*(*arr[10])(void ))(void );

(yes I had to cheat and ran cdecl> declare arr as array 10 of pointer to function(void) returning pointer to function(void) returning pointer to char )





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签