某类函数返回值是指针,怎样以指向函数的指针为参数调用这类函数

void show(struct _Node (*fun)(struct _Node *head))
{
struct _Node *pTemp = NULL;
pTemp = (*fun)(head); //就是这里vc提示head无定义,那么该怎样调用呢
这个函数类是型如 struct _Node *QueryQQ(struct _Node *head) 的一类函数

你把head从函数里面拿出来,作为参数就可以了啊。

之前需要定义一个函数指针:typedef struct _Node (*Fun)(struct _Node *p);
然后定义show函数:
void show(Fun fun, struct _Node *head)
{
struct _Node *pTemp = NULL;

pTemp = fun(head);

}
这样就可以把head传进来了~追问

error。 binary '=' : no operator defined which takes a right-hand operand of type 'struct _Node' (or there is no acceptable conversion)
还是有错,我不明白是怎么回事

追答

哦,应该是 *pTemp = fun(head);因为你定义的fun函数是返回_Node而不是_Node*,或者你修改你下的函数,返回指针就可以了~

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-16
在这个定义里
struct _Node (*fun)(struct _Node *head)

head是函数指针的形参,是没有意义的,等价于
struct _Node (*fun)(struct _Node *)
你想想你要调用一个函数,你能把形参的名字拿来用么:
int f(int a){
......
}
int main() {
f(a); // 这肯定不行啊

}
不知道你这个函数指针有什么用,所以无法猜出正确的应该怎么写。追问

其实就是为了简化一下代码,不想写那么冗长

相似回答
大家正在搜