C为什么函数指针作为参数而不是一个函数? [英] C Why function pointer as parameter instead of just a function?
问题描述
我一直在阅读有关函数作为参数的函数,特别是在C中,它们使用函数指针。假设我想实现牛顿raphson方法(以一种简单的方式)计算非线性方程中的零点。
double derivative (double f(double),double x)
{
double h = 1e-9;
return(f(x + h) - f(x))/ h;
double newton_raphson(double f(double),double x0,double tol)
{
double xk,diff;
do
{
xk = x0 - f(x0)/ derivative(f,x0);
diff = fabs(xk - x0);
x0 = xk;
} while(diff> = tol);
return xk;
$ b因此,为了计算导数的近似值,我需要一个函数返回一个double并以双重作为论点。给定其他参数,计算函数的根也是一样。我的问题是,为什么这与将函数参数声明为函数指针不同?例如将输入参数f声明为函数指针而不是函数...
解决方案 参数 f
是派生和 newton_raphson
中的指针函数。
double derivative(double f(double),double x){...}
完全等价于
double derivative( double(* f)(double),double x){...}
只有前者看起来更好 - 通常当你可以省略括号时,你应该这样做。毕竟它们都相当于
double((((derivative)))(double(((*(f) )))(double((trouble))),double((x)))){...}
我希望只能在IOCCC中使用。然而,如果你声明,定义一个变量(不是函数参数),你需要使用
double(* f)(double);
as
double f(double);
仅仅是一个函数声明。
6.7.6.3函数声明符(包括原型) C11 draft n1570说:
参数声明为'返回
类型
'的函数'应调整为''指向
函数返回
类型
' ',如6.3.2.1。
和
6.9.1函数定义进一步说明那么
[...]每个参数的类型按照参数类型列表的6.7.6.3中的描述进行调整;结果类型应该是一个完整的对象
类型。
另外它有以下例子:
示例2
要将一个函数传递给另一个函数,
int f(void);
/ * ... * /
g(f);
然后, g
的定义可能会读取
void g(int(* funcp)(void))
{
/ * ... *
(* funcp)(); / *或funcp(); ... * /
}
或等同地
void g(int func(void))
{
/ * ... * /
func(); / *或(* func)(); ... * /
}
I have been reading about having functions with functions as parameters, and particulary in C, they use function pointers. Let's suppose I want to implement the newton raphson method (in a simple way) for computing zeros in non linear equations.
double derivative(double f(double), double x)
{
double h = 1e-9;
return (f(x + h) - f(x)) / h;
}
double newton_raphson(double f(double), double x0, double tol)
{
double xk, diff;
do
{
xk = x0 - f(x0) / derivative(f, x0);
diff = fabs(xk - x0);
x0 = xk;
} while (diff >= tol);
return xk;
}
So, to compute an approximation for derivative I need a function that returns a double and takes a double as an argument. Same for computing a root of the function, given the other parameters. My question is, why is this different from declaring function parameters as function pointers? For instance declaring the input parameter f as a function pointer instead of just a function...
解决方案 The parameter f
is a pointer-to-function in both derivative
and newton_raphson
.
double derivative(double f(double), double x) { ... }
is exactly equivalent to
double derivative(double (*f)(double), double x) { ... }
Only, the former looks nicer - usually when you can omit parentheses, you should probably do so. After all both of them are equivalent to
double ((((derivative)))(double (((*(f))))(double ((trouble))), double ((x)))) { ... }
That I hope will only ever be used in IOCCC.
However, if you're declaring, defining a variable (not a function parameter), you need to use
double (*f)(double);
as
double f(double);
is just a function declaration.
6.7.6.3 Function declarators (including prototypes) of C11 draft n1570 says:
A declaration of a parameter as ‘‘function returning
type
’’ shall be adjusted to ‘‘pointer to
function returning
type
’’, as in 6.3.2.1.
And
6.9.1 Function definitions further says that
[...] the type of each parameter is adjusted as described in 6.7.6.3 for a parameter type list; the resulting type shall be a complete object
type.
additionally it has the following example:
EXAMPLE 2
To pass one function to another, one might say
int f(void);
/* ... */
g(f);
Then the definition of g
might read
void g(int (*funcp)(void))
{
/* ... *
(*funcp)(); /* or funcp(); ... */
}
or, equivalently,
void g(int func(void))
{
/* ... */
func(); /* or (*func)(); ... */
}
这篇关于C为什么函数指针作为参数而不是一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文