从 C/C++ 中的函数返回函数指针
在 C/C++ 中,像普通数据指针(int *、char * 等)一样,可以有指向函数的指针。在程序中创建的每个函数都会在内存中获得一个地址,因为指针可以在 C/C++ 中使用,因此也可以创建指向函数的指针。
句法:
return type (*function_pointer_name) (argument_type_1, argument_type_2, ……, argument_type_n) = &function_name;
OR
return type (*function_pointer_name) (argument_type_1, argument_type_2, ……, argument_type_n) = function_name;
注意:函数指针的参数类型和返回类型应与程序中存在的实际函数匹配。
方案一:
C
// C program for the above approach
#include
// Function to add the value 10 to
// the variable a
void demo(int* a) { *a += 10; }
// Driver Code
int main()
{
int num = 20;
// ptr contains address of demo
// function or void
void (*ptr)(int*) = &demo;
// or (*ptr)(&num);
ptr(&num);
printf("%d", num);
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
void demo(int& a)
{
a += 10;
}
// Driver Code
int main()
{
int num = 20;
// Now ptr contains address of demo
// function or void
void (*ptr)(int*) = &demo;
// or (*ptr)(num);
ptr(num);
cout << num << endl;
return 0;
}
C
// C program for the above approach
#include
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
printf("geeks!!\n");
return *y + 10;
}
// Function that return type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
printf("%d", (*u())(&a));
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
cout << "geeks!!" << endl;
return *y + 10;
}
// Function that returns the type ptr
ptr fun()
{
cout << "Hello ";
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
cout << (*u())(&a) << endl;
return 0;
}
C
// C program for the above approach
#include
// This defines a type for
// function prototypes
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
printf("geeks!!\n");
return *y + 10;
}
// fun() is a function with
// return type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver code
int main()
{
int a = 10;
pm u = fun;
/*
Above line assigns 'u' which is
of type 'pm' to an array of size
1 which has function pointers as
its elements and these function
pointers in turn return other
function pointer which points to
other functions.
Now this 'p' array contains a function
pointer 'u' which points to fun() and
this fun() returns another function
pointer which points to fun1().
*/
int (*(*p[1])())(int*) = { u };
printf("%d", (*p[0]())(&a));
}
C++
// C++ program for the above approach
#include
using namespace std;
// This defines a type for
// function prototypes
typedef void (*ptr)(int&);
typedef ptr (*pm)();
void fun1(int& z)
{
printf("geeks!!\n");
cout << z + 10 << endl;
}
// Function that returns type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
/*
Above line assigns 'u' which is
of type 'pm' to an array of size
1 which has function pointers as its
elements and these function pointers
in turn return other function pointer
which points to other functions.
Now this 'p' array contains a function
pointer 'u' which points to fun() and
this fun() returns another function
pointer which points to fun1() and
this fun1() returns void.
*/
void (*(*p[1])())(int&) = { u };
(*p[0]())(a);
}
30
返回函数指针从函数:从一个函数返回一个函数指针,函数的返回类型应该是一个指向另一个函数。但是编译器不接受函数的这种返回类型,因此我们需要定义一个表示该特定函数指针的类型。
句法 :
typedef return type (*function_pointer_name) (argument_type_1, argument_type_2, ……, argument_type_n);
This creates a type which represents a pointer for a particular function.
方案二:
C
// C program for the above approach
#include
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
printf("geeks!!\n");
return *y + 10;
}
// Function that return type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
printf("%d", (*u())(&a));
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
cout << "geeks!!" << endl;
return *y + 10;
}
// Function that returns the type ptr
ptr fun()
{
cout << "Hello ";
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
cout << (*u())(&a) << endl;
return 0;
}
Hello geeks!!
20
宣称具有两个函数指针作为其元素的,并且这些函数指针阵列,反过来,返回其它函数指针,其指向其它功能。驱动代码main()函数的逻辑可以在上面的程序中改成:
方案三:
C
// C program for the above approach
#include
// This defines a type for
// function prototypes
typedef int (*ptr)(int*);
typedef ptr (*pm)();
int fun1(int* y)
{
printf("geeks!!\n");
return *y + 10;
}
// fun() is a function with
// return type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver code
int main()
{
int a = 10;
pm u = fun;
/*
Above line assigns 'u' which is
of type 'pm' to an array of size
1 which has function pointers as
its elements and these function
pointers in turn return other
function pointer which points to
other functions.
Now this 'p' array contains a function
pointer 'u' which points to fun() and
this fun() returns another function
pointer which points to fun1().
*/
int (*(*p[1])())(int*) = { u };
printf("%d", (*p[0]())(&a));
}
C++
// C++ program for the above approach
#include
using namespace std;
// This defines a type for
// function prototypes
typedef void (*ptr)(int&);
typedef ptr (*pm)();
void fun1(int& z)
{
printf("geeks!!\n");
cout << z + 10 << endl;
}
// Function that returns type ptr
ptr fun()
{
printf("Hello ");
// or return fun1;
/* or
int(*pt)(int*)=fun1;
return pt
*/
return &fun1;
}
// Driver Code
int main()
{
int a = 10;
pm u = fun;
/*
Above line assigns 'u' which is
of type 'pm' to an array of size
1 which has function pointers as its
elements and these function pointers
in turn return other function pointer
which points to other functions.
Now this 'p' array contains a function
pointer 'u' which points to fun() and
this fun() returns another function
pointer which points to fun1() and
this fun1() returns void.
*/
void (*(*p[1])())(int&) = { u };
(*p[0]())(a);
}
Hello geeks!!
20