C函数指针的硬伤 [英] C Function Pointer Mishap

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

问题描述

好了,所以我努力学习函数指针。我有一个基本的函数指针的设置像这样。

Okay so I'm trying to learn function pointers. I have a basic function pointer setup like so.

功能打印出链表:

void seq_print(Seq seq, void (* print_func)(void *)){
Node * p = seq->top;
    if(p == NULL){
        printf("%s %c", "There is no data to print.", '\n');
        return;
    }
    while(p != NULL){
        print_func(p->data);
        p = p->next;
    }
}

测试功能:

seq_print(s, printFunc(1));

我得到这个错误:

I get this error:

seq.h:113:32: error: expected declaration specifiers or ‘...’ before ‘(’ token
 extern void seq_print(Seq seq, (void *) print_func(void *));

我真的不知道该怎么做,任何见解将是有益的。

I'm really not sure what to do, any insight would be helpful.

推荐答案

您的两个错误:

首先,错误消息通知声明:在你的头文件 seq.h 的函数声明是错误的!

First, notice declaration in error message: in your header file seq.h, declaration of function is wrong!

 extern void seq_print(Seq seq, (void *) print_func(void *));
 //                             ^      ^ wrong = parenthesis return type

应该是:

 extern void seq_print(Seq seq, void (*print_func) (void *));
 //                                  ^ correct   ^ = parenthesis function name 

其次,在调用的地方。

Second, at calling place.

seq_print(s, printFunc(1));
//                    ^^^ you are calling function, and passes returned value 

应该是:

seq_print(s, printFunc);
//           ^^^^^^^^^^ don't call pass function address

我的关注code例子可以帮助你更好地理解(阅读评论)

My following code examples will help you to understand better (read comments):

#include<stdio.h>
void my_g2(int i, (void*) f(int));  // Mistake: Notice () around void*
void f(int i){
    printf("In f() i =  %d\n", i);        
}
int main(){
    my_g2(10, f(1));  // wrong calling
    return 0;
}
void my_g2(int i, void (*f)(int)){
    printf("In g()\n");
    f(i);
}

codePAD 为工作code。你可以看到的错误是类似于你做了什么:

Check codepad for working code. You can see error is similar to what you are getting:

Line 2: error: expected declaration specifiers or '...' before '(' token
In function 'main':
Line 8: error: too many arguments to function 'my_g2'

现在正确版本的code的:

Now correct version of this code:

#include<stdio.h>
void my_g2(int i, void (*f)(int)); // Corrected declaration 
void f(int i){
    printf("In f() i =  %d\n", i);
}
int main(){
    my_g2(10, f);  // corrected calling too 
    return 0;
}
void my_g2(int i, void (*f) (int)){
    printf("In g()\n");
    f(i);
}

现在检查 codepade 输出:

In g()
In f() i =  10

编辑:添加评论的基础上

但如果它像无效(* F)(无效*)我怎么传递值吗?

But what if it's like void (*f) (void *) how do I pass in values to that?

从调用函数main()(在我的例子= my_g2 ),你需要通过你想在我的例子调用(函数指针 F())的功能,您在主调用(即 my_g2 )。

From calling function in main() (in my example = my_g2) you need to pass function pointer which you wants call (in my example f()) from function you calls in main (that is my_g2).

你想叫 F() my_g2()

我们总是传递参数的函数调用的时候发挥作用。所以,如果你想传递参数给 F()功能,你必须通过当你调用这个在 my_g2()

We always pass parameters to function at the time of function calling. So if you wants to pass parameters to f() function you have to pass when you call this in my_g2().

一个呼叫前pression像下面(阅读评论)

A calling expression like below (read comments):

seq_print(s, printFunc(1));
             ^ // first printFunc(1) will be called then seq_prints
             pass returned value from printFunc(1)

是错误的,因为如果这样做 seq_print 将与第二放慢参数值调用=从函数的返回值 printFunc(1)

is wrong because if you do so seq_print will be called with second paramter value = returned value from function printFunc(1).

要通过空指针,我下面code可以帮助你进一步的:

To pass void pointer, my following code may help you further:

#include<stdio.h>
void my_g2(void* i, void (*f)(void*));
void f(void *i){
    printf("In f(), i =  %d\n", *(int*)i);
    *(int*)i = 20; 
}
int main(){
    int i = 10; 
    my_g2(&i, f);
    printf("Im main, i = %d", i);
    return 0;
}
void my_g2(void* i, void (*f)(void*)){
    printf("In g()\n");
    f(i);
}

输出@ codepade

In g()
In f(), i =  10
Im main, i = 20

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

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