如果未初始化,以函数指针作为参数的函数如何接受它?
How do functions with a function pointer as an argument accept it if it is uninitialized?
any_function声明中的函数指针如何接收任何值?在 main 函数中,any_function 从未被赋予任何函数指针,只有函数本身。这是否意味着函数指针 any_function 正在递归调用自身?
#include <stdio.h>
int sum(int, int);
int product(int, int);
int difference(int, int);
int any_function(int(*pfun)(int, int), int x, int y);
int main(void)
{
int a = 10; /* Initial value for a */
int b = 5; /* Initial value for b */
int result = 0; /* Storage for results */
int (*pf)(int, int) = sum; /* Pointer to function */
/* Passing a pointer to a function */
result = any_function(pf, a, b);
printf("\nresult = %d", result );
/* Passing the address of a function */
result = any_function(product,a, b);
printf("\nresult = %d", result );
printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}
/* Definition of a function to call a function */
int any_function(int(*pfun)(int, int), int x, int y)
{
return pfun(x, y);
}
/* Definition of the function sum */
int sum(int x, int y)
{
return x + y;
}
/* Definition of the function product */
int product(int x, int y)
{
return x * y;
}
/*Defintion of the function product*/
int difference(int x, int y)
{
return x - y;
}
How does the function pointer in the declaration of any_function receive any value?
因为函数调用指定了一个值。与任何其他参数相同。
例如当你写:
int my_function(int x) {
return x + 1;
}
// in main
printf("%d\n", my_function(5));
它打印出 6。但是 my_function
的声明中的 x
是如何接收到任何值的呢?嗯,写my_function(5)
.
的时候就指定了
In the main function, any_function is never given any function pointers, only functions themselves.
如果您使用这样的函数,它会自动转换为指针。换句话说 pf = sum;
是 pf = ∑
的缩写
Does this mean the function pointer any_function is recursively calling itself?
不是,这个想法是从哪里来的?
any_function声明中的函数指针如何接收任何值?在 main 函数中,any_function 从未被赋予任何函数指针,只有函数本身。这是否意味着函数指针 any_function 正在递归调用自身?
#include <stdio.h>
int sum(int, int);
int product(int, int);
int difference(int, int);
int any_function(int(*pfun)(int, int), int x, int y);
int main(void)
{
int a = 10; /* Initial value for a */
int b = 5; /* Initial value for b */
int result = 0; /* Storage for results */
int (*pf)(int, int) = sum; /* Pointer to function */
/* Passing a pointer to a function */
result = any_function(pf, a, b);
printf("\nresult = %d", result );
/* Passing the address of a function */
result = any_function(product,a, b);
printf("\nresult = %d", result );
printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}
/* Definition of a function to call a function */
int any_function(int(*pfun)(int, int), int x, int y)
{
return pfun(x, y);
}
/* Definition of the function sum */
int sum(int x, int y)
{
return x + y;
}
/* Definition of the function product */
int product(int x, int y)
{
return x * y;
}
/*Defintion of the function product*/
int difference(int x, int y)
{
return x - y;
}
How does the function pointer in the declaration of any_function receive any value?
因为函数调用指定了一个值。与任何其他参数相同。
例如当你写:
int my_function(int x) {
return x + 1;
}
// in main
printf("%d\n", my_function(5));
它打印出 6。但是 my_function
的声明中的 x
是如何接收到任何值的呢?嗯,写my_function(5)
.
In the main function, any_function is never given any function pointers, only functions themselves.
如果您使用这样的函数,它会自动转换为指针。换句话说 pf = sum;
是 pf = ∑
Does this mean the function pointer any_function is recursively calling itself?
不是,这个想法是从哪里来的?