gpt4 book ai didi

c - 返回对 C 中函数指针的调用

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:23 25 4
gpt4 key购买 nike

摘自《21st Century C》一书:

Conceptually, the syntax for a function type is really a pointer to a function of a given type. If you have a function with a header like:

double a_fn(int, in); //a declaration

then just add a star (and parens to resolve precedence) to describe a pointer to this type of function:

 double (*a_fn_type)(int, int); //a type: pointer-to-function

Then put typedef in front of that to define a type:

 typedef double (*a_fn_type)(int, int); //a typedef for a pointer to function

Now you can use it as a type like any other, such as to declare a function that takes another function as input:

double apply_a_fn(a_fn_type f, int first_in, int second_in){
return f(first_in, second_in); //shouldn't this be *f(first_in, second_in) ?
}

问题最后一个函数的返回值不应该是*f(first_in, second_in)吗,因为f是一个指向函数的指针和 *f 表示实际函数?

最佳答案

调用函数指针的解引用运算符是optional in C .

关于c - 返回对 C 中函数指针的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com