在c中写入一个函数指针 [英] Writing a function pointer in c

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

问题描述

  int(* fn_pointer(this_args(this_args))这个函数指针被写为: ))(this_args)

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

  return_type(* fn_pointer)(arguments); 

讨论类似的事情

  / /这是一个名为functionFactory的函数,它接收参数n 
//并返回一个指向另一个函数的指针,该函数接收两个ints
//并返回另一个int
int(* functionFactory(int n ))(int,int){
printf(Got parameter%d,n);
int(* functionPtr)(int,int)=& addInt;
返回functionPtr;
}

有人可以告诉我有什么不同,以及它是如何工作的? p>

解决方案

int(* fn_pointer(this_args))(this_args);

声明 fn_pointer 是一个函数, code> this_args 并返回一个指向一个函数的指针,该函数以 this_args 作为参数,并返回一个 int 类型。它相当于

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

让我们多了解一下:

  int f1(arg1,arg2); // f1是一个函数,它接受两个类型为
// arg1和arg2的参数并返回一个int。

int * f2(arg1,arg2); // f2是一个函数,它接受两个类型为
// arg1和arg2的参数并返回一个指向int的指针。

int(* fp)(arg1,arg2); // fp是一个指向函数的指针,该函数接受两个类型为
// arg1和arg2的参数,并返回一个指向int的指针。

int f3(arg3,int(* fp)(arg1,arg2)); // f3是一个函数,它接受
的两个参数//类型arg3和一个函数指针
//接受两个类型为arg1和arg2的参数和
// //返回一个int 。

int(* f4(arg3))(arg1,arg2); // f4是一个函数,它接受
// arg3类型的参数并返回一个指向一个函数的指针,该函数接受两个
//类型为arg1和arg2的参数并返回一个int

如何阅读 int(* f4(arg3))(arg1,arg2);

  f4  -  f4 
f3() - 是一个函数
f3(arg3) - 接受一个arg3参数
* f3(arg3) - 返回一个指针
(* f3(arg3))() - 函数
(* f3(arg3))(arg1,arg2) - 取arg1和arg2参数
int(* f3(arg3))( arg1,arg2) - 并返回一个int

所以,最后一个家庭作品:)。尝试找出声明

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

并使用 typedef 重新定义它。 / p>

I was recently reading a code, and found that a function pointer is written as :

int (*fn_pointer ( this_args ))( this_args )

I usually encounter a function pointer like this :

return_type (*fn_pointer ) (arguments);

Similar thing is discussed 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;
}

Can somebody tell me what is the difference and how does this work ?

解决方案

int (*fn_pointer ( this_args ))( this_args );  

declares fn_pointer as a function that takes this_args and returns a pointer to a function that takes this_args as argument and returns an int type. It is equivalent to

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

Let's understand it a bit more:

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 read int (*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  

So, finally a home work :). Try to figure out the declaration

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

and use typedef to redefine it.

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

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