【问题标题】:Writing a function pointer in c用c编写函数指针
【发布时间】:2015-02-18 12:37:36
【问题描述】:

最近在看代码,发现函数指针写成:

int (*fn_pointer ( this_args ))( this_args )

我经常遇到这样的函数指针:

return_type (*fn_pointer ) (arguments);

类似的事情在讨论here:

// this is a function called functionFactory which receives parameter n
// and returns a pointer to another function which receives two ints
// and it returns another int
int (*functionFactory(int n))(int, int) {
printf("Got parameter %d", n);
int (*functionPtr)(int,int) = &addInt;
return functionPtr;
}

谁能告诉我有什么区别以及它是如何工作的?

【问题讨论】:

标签: c function function-pointers


【解决方案1】:
int (*fn_pointer ( this_args ))( this_args );  

fn_pointer 声明为一个接受this_args 的函数并返回一个指向以this_args 作为参数并返回int 类型的函数的指针。相当于

typedef int (*func_ptr)(this_args);
func_ptr fn_pointer(this_args);

让我们再理解一下:

int f1(arg1, arg2);  // f1 is a function that takes two arguments of type   
                     // arg1 and arg2 and returns an int.

int *f2(arg1, arg2);  // f2 is a function that takes two arguments of type  
                      // arg1 and arg2 and returns a pointer to int.  

int (*fp)(arg1, arg2); // fp is a pointer to a function that takes two arguments of type  
                       // arg1 and arg2 and returns a pointer to int.  

int f3(arg3, int (*fp)(arg1, arg2)); // f3 is a function that takes two arguments of  
                                        // type arg3 and a pointer to a function that 
                                        // takes two arguments of type arg1 and arg2 and 
                                        // returns an int.  

int (*f4(arg3))(arg1, arg2); // f4 is a function that takes an arguments of type   
                             // arg3 and returns a pointer to a function that takes two 
                             // arguments of type arg1 and arg2 and returns an int   

How to readint (*f4(arg3))(arg1, arg2);

          f4                           -- f4
        f3(   )                        -- is a function
        f3(arg3)                       --  taking an arg3 argument
       *f3(arg3)                       --   returning a pointer
     (*f3(arg3))(    )                 --   to a function
    (*f3(arg3))(arg1, arg2)            --     taking arg1 and arg2 parameter
  int (*f3(arg3))(arg1, arg2)           --     and returning an int  

所以,最后是家庭作业:)。试着弄清楚声明

void (*signal(int sig, void (*func)(int)))(int);  

并使用typedef 重新定义它。

【讨论】:

  • 哪个是argument,哪个是return?
【解决方案2】:

来自cdecl(这是一个方便的帮助工具来破译 C 声明):

int (*fn_pointer ( this_args1 ))( this_args2 )

将 fn_pointer 声明为函数 (this_args1) 返回指向的指针 函数 (this_args2) 返回 int

因此前者是一个函数,它返回指向函数的指针,而后者:

return_type (*fn_pointer ) (arguments);

是一个普通的“函数指针”。


Clockwise/Spiral Rule 文章中阅读更多关于不理解复杂声明的信息。

【讨论】:

  • fn_pointer 不是此声明中的指针(只是一个函数)。
【解决方案3】:

这个

int (*fn_pointer ( this_args1 ))( this_args2 )

声明一个以this_args1为参数并返回类型为函数指针的函数

int (*fn_pointer)(this_args2)

所以它只是一个返回函数指针的函数。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多