如何在以下代码中将参数传递给函数指针?
How can I pass arguments to the function pointer in following code?
我尝试了这些代码,但参数 a 和 b 的范围存在一些问题。请有人帮助我。
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
void passptr(int( * fp)(int a, int b)) {
int result = ( * fp)(a, b);
printf("%d", result);
}
int main() {
add(3, 5);
passptr( & add);
getch();
return 0;
}
void passptr(int (*fp)(int a,int b))
{
int result=(*fp)(a,b);
printf("%d",result);
}
a
和 b
这里只是您为函数指针的参数赋予的描述性名称,它们不是声明变量。只需使用有效参数调用函数:
//call with literals
int result = (*fp)(1,2);
//or variables
int a = 1;
int b = 2;
int result = (*fp)(a,b);
//or pass as arguments
void passptr(int (*fp)(int,int), int a, int b) {
int result = (*fp)(a,b)
}
//and call like this
passptr(&add, 1, 2);
你想要这个:
void passptr(int (*fp)(int, int), int a, int b) // corrected argument list
{
int result=(*fp)(a,b);
printf("%d",result);
}
int main()
{
add(3,5); // this is useless leftover from your original code
passptr(&add, 3, 5); // added parameters 3 and 5 here
getch();
return 0;
}
在您的 void passptr(int( * fp)(int a, int b))
中,a
和 b
不是 passptr
函数的参数,只是占位符。
如果你typedef
你的函数指针:
,这种调用更容易理解
#include<conio.h>
#include<stdio.h>
// addFuncPtr_t is a pointer to a function that:
// - returns int
// - takes two int arguments
typedef int ( *addFuncPtr_t )( int, int );
int add(int x, int y) {
return (x + y);
}
void passptr(addFuncPtr_t fp, int a, int b) {
int result = fp(a, b);
printf("%d", result);
}
int main() {
add(3, 5);
// note that the function is passed separately from
// the arguments - add(3,5) would *call* the function
// instead of passing the address of the function
passptr( add, 3, 5 );
getch();
return 0;
}
您可能想试试这个:
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
void passptr(int( * fp)(int a, int b)) {
int result = ( * fp)(3, 5);
printf("%d", result);
}
int main() {
add(3, 5);
passptr( & add);
getch();
return 0;
}
如果您使用的是 C++,使用模板会简单得多:
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
template<typename funcPtr>
void passptr(funcPtr f,int a, int b) {
int result = f(a, b);
printf("%d", result);
}
int main() {
passptr(add,3,5);
return 0;
}
我尝试了这些代码,但参数 a 和 b 的范围存在一些问题。请有人帮助我。
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
void passptr(int( * fp)(int a, int b)) {
int result = ( * fp)(a, b);
printf("%d", result);
}
int main() {
add(3, 5);
passptr( & add);
getch();
return 0;
}
void passptr(int (*fp)(int a,int b))
{
int result=(*fp)(a,b);
printf("%d",result);
}
a
和 b
这里只是您为函数指针的参数赋予的描述性名称,它们不是声明变量。只需使用有效参数调用函数:
//call with literals
int result = (*fp)(1,2);
//or variables
int a = 1;
int b = 2;
int result = (*fp)(a,b);
//or pass as arguments
void passptr(int (*fp)(int,int), int a, int b) {
int result = (*fp)(a,b)
}
//and call like this
passptr(&add, 1, 2);
你想要这个:
void passptr(int (*fp)(int, int), int a, int b) // corrected argument list
{
int result=(*fp)(a,b);
printf("%d",result);
}
int main()
{
add(3,5); // this is useless leftover from your original code
passptr(&add, 3, 5); // added parameters 3 and 5 here
getch();
return 0;
}
在您的 void passptr(int( * fp)(int a, int b))
中,a
和 b
不是 passptr
函数的参数,只是占位符。
如果你typedef
你的函数指针:
#include<conio.h>
#include<stdio.h>
// addFuncPtr_t is a pointer to a function that:
// - returns int
// - takes two int arguments
typedef int ( *addFuncPtr_t )( int, int );
int add(int x, int y) {
return (x + y);
}
void passptr(addFuncPtr_t fp, int a, int b) {
int result = fp(a, b);
printf("%d", result);
}
int main() {
add(3, 5);
// note that the function is passed separately from
// the arguments - add(3,5) would *call* the function
// instead of passing the address of the function
passptr( add, 3, 5 );
getch();
return 0;
}
您可能想试试这个:
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
void passptr(int( * fp)(int a, int b)) {
int result = ( * fp)(3, 5);
printf("%d", result);
}
int main() {
add(3, 5);
passptr( & add);
getch();
return 0;
}
如果您使用的是 C++,使用模板会简单得多:
#include<conio.h>
#include<stdio.h>
int add(int x, int y) {
return (x + y);
}
template<typename funcPtr>
void passptr(funcPtr f,int a, int b) {
int result = f(a, b);
printf("%d", result);
}
int main() {
passptr(add,3,5);
return 0;
}