需要帮助理解函数指针作为参数语法? [英] Need help understanding function pointers as arguments syntax?

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

问题描述

需要帮助理解将函数指针作为参数传递的语法

* f前面的第一个双精度是什么意思?

  double  integration( double (* f)( double ), double  a, double  b); 



如果我打电话的话如下:

 result = integrate(sin,0.0,PI / 2); 



我知道sin生成的指针作为参数传递给积分,0.0被分配给'a',PI / 2被赋值给'b'。当然,这只是一个场景,PI已经宣布。我正在读一本书并且作者从未解释过* f之后的第一个双重意味着什么,即使它是一个论证,是指从罪中得到指针的论证?如果这是为什么它不是*双而是?我需要解释* f之后的第一个双重内容。谢谢。



另外,如果有更好的方法来写这个,请告诉我,我可以把它放在我的笔记中。

解决方案

让我们打破双倍(* f)(双倍)分开;



这里的括号内的星号表示它是一个函数指针(* f) f 是我们的名字给它,就像一个是你的例子中第二个参数的名称。



类型(或在(* f)是方法的参数签名之后,如果它指向具有多个参数的方法的类型,在这种情况下,(double),表示它指向一个方法,该方法采用类型 double 的单个参数。



(* f)之前出现的前导类型是指向的函数的返回类型,在此case double



所以通过全部o如上所述,你可以读到 f 是一个指向一个函数的指针,该函数将一个 double 作为参数并返回一个。并且 sin 就是这样一种方法。



函数指针需要具体到足以详细说明传递的参数,以及函数的返回值指向。



这几乎是 C 中表达式函数指针的标准方式。 />


希望这会有所帮助,

Fredrik


只是在处理函数时向解决方案1添加内容指针,通常是 typedef 很方便。例如,请参阅:以艰难的方式学习C:练习18:指向函数的指针 [ ^ ]。


一点是缺少澄清为什么函数指针始终在圆括号中:

括号驱动编译器将'* f'作为函数指针而不是指向结果类型的指针。

即:

  void  * f( int  a);  //  这是函数f的原型,返回指向void的指针 
int * f( int a); // 这是函数f的原型,返回指向int的指针
... 。 // 依此类推......
double (* f)( int a); // 这是一个指向函数的指针,因为星号是
// 引用函数名称,作为函数的指针,
// 没有别的!
int (* af [] )( int a); // 这是什么?!? :D
// 这是一个以int为参数的函数指针数组
// 并返回一个int。


Just need help understanding the syntax of passing a function pointer as an argument
What does the very first double in front of the *f mean?

double integrate(double(*f)(double) , double a , double b);


If i for example call something like:

result = integrate(sin, 0.0, PI/2);


I know sin's produced pointer is being passed as an argument to integrate, and 0.0 gets assigned to 'a', and PI/2 gets assigned to 'b'. Of course this is just a scenario, where PI would be declared already. I am reading this off a book and the author never explains what the first double after *f means, even if it is an argument, is that the argument that takes the pointer from sin? If it is why is it not *double instead? I need an explanation for what the first double after *f does. Thank You.

Also, if there is a better way to write this, please let me know so i can put it in my notes.

解决方案

Let's break double(*f)(double) apart;

The asterisk inside parenthesis here indicates that it's a function pointer (*f), the f is the name we give it, just like a is the name of the second parameter in your example.

The type (or types if it's pointing to a method with more than one argument) in parenthesis after the (*f) is the argument signature of the method, in this case, (double), means it's pointing to a method that takes a single argument of type double.

The leading type that appears before the (*f) is the return type of the function pointed to, in this case double.

So by putting all of the above together you can read that f is a pointer to a function taking a single double as argument and returning a double. And sin is such a method.

The function pointer needs to be specific enough to detail both the arguments passed, and the return value of the function pointed to.

This is pretty much the standard way of expression function pointers in C.

Hope this helps,
Fredrik


Just to add something to solution 1, when dealing with function pointers, usually a typedef is handy. See, for instance: "Learn C the hard way: Exercise 18: Pointers To Functions"[^].


One point is missing to clarify why a function pointer is always in round brackets:
The brackets drive the compiler to take '*f' as a function pointer not a pointer to the result type.
I.e:

void *f(int a);      //This is a prototype for a function f returning a pointer to void
int  *f(int a);      //This is a prototype for a function f returning a pointer to int
....                 //and so on...
double (*f)(int a);  //This is a pointer to function, because the asterisk is to be
                     //referred to function name, as a pointer to function,
                     //and nothing else!
int  (*af[])(int a); //What's this?!? :D
                     //It's an array of function pointers taking an int as parameter
                     //and returning an int.


这篇关于需要帮助理解函数指针作为参数语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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