下面的代碼展示了如何聲明一個指向一個函數(shù)的指針。
int (*pfunction) (int);
上面的代碼聲明一個變量,它是一個指向函數(shù)的指針。
這個語句只是定義了指針變量。
指針的名稱是 function
,它指向的函數(shù)一個int類型的參數(shù)和一個int返回類型。
假設(shè)您定義了一個具有以下原型的函數(shù):
int sum(int a, int b);
該函數(shù)有兩個類型為int的參數(shù),返回類型為int。我們可以為它定義一個函數(shù)指針,如下所示。
int (*pfun)(int, int) = sum;
這聲明一個名為pfun的函數(shù)指針。
它可以存儲具有兩個參數(shù)的函數(shù)的地址類型int和類型int的返回值。
該語句還使用函數(shù) sum()
的地址初始化 pfun
。
你可以通過函數(shù)指針調(diào)用sum(),如下所示:
int result = pfun(4, 5);
這個語句通過pfun指針調(diào)用sum()函數(shù)參數(shù)值為4和5。
我們可以像函數(shù)名一樣使用函數(shù)指針名稱。
假設(shè)您定義了另一個具有以下原型的函數(shù):
int divide(int a, int b);
您可以使用以下語句存儲pfun中的divide()的地址:
pfun = divide;
使用包含divide()的地址的 pfun
,您可以通過指針調(diào)用divide():
result = pfun(5, 12);
以下代碼具有三個具有相同參數(shù)的函數(shù)并返回類型并使用指向函數(shù)的指針來調(diào)用它們。
#include <stdio.h>
//from w ww . ja v a 2 s . co m
// Function prototypes
int sum(int, int);
int product(int, int);
int difference(int, int);
int main(void) {
int a = 10;
int b = 5;
int result = 0;
int (*pfun)(int, int); // Function pointer declaration
pfun = sum; // Points to function sum()
result = pfun(a, b); // Call sum() through pointer
printf("pfun = sum result = %2d\n", result);
pfun = product; // Points to function product()
result = pfun(a, b); // Call product() through pointer
printf("pfun = product result = %2d\n", result);
pfun = difference; // Points to function difference()
result = pfun(a, b); // Call difference() through pointer
printf("pfun = difference result = %2d\n", result);
return 0;
}
int sum(int x, int y) {
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int difference(int x, int y)
{
return x - y;
}
上面的代碼生成以下結(jié)果。
您可以創(chuàng)建一個指向函數(shù)的指針數(shù)組。
要聲明一個函數(shù)指針數(shù)組,將數(shù)組維度放在函數(shù)指針數(shù)組名稱后面。
int (*pfunctions[10]) (int);
這聲明一個數(shù)組,函數(shù),有十個元素。
此數(shù)組中的每個元素都可以存儲a的地址函數(shù),返回類型為int,參數(shù)類型為int。
下面的代碼顯示了如何使用指針數(shù)組到功能。
#include <stdio.h>
//from w w w . j av a 2 s .com
// Function prototypes
int sum(int, int);
int product(int, int);
int difference(int, int);
int main(void) {
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int); // Function pointer array declaration
// Initialize pointers
pfun[0] = sum;
pfun[1] = product;
pfun[2] = difference;
// Execute each function pointed to
for(int i = 0 ; i < 3 ; ++i) {
result = pfun[i](a, b); // Call the function through a pointer
printf("result = %2d\n", result);
}
// Call all three functions through pointers in an expression
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("The product of the sum and the difference = %2d\n", result);
return 0;
}
int sum(int x, int y) {
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int difference(int x, int y)
{
return x - y;
}
上面的代碼生成以下結(jié)果。
您可以使用函數(shù)的指針作為函數(shù)的參數(shù)。
這樣我們可以調(diào)用不同的函數(shù)這取決于指針指向哪個函數(shù)。
#include <stdio.h>
/*from w ww . j av a 2 s .c om*/
// Function prototypes
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;
int b = 5;
int result = 0;
int (*pf)(int, int) = sum; // Pointer to function
// Passing a pointer to a function
result = any_function(pf, a, b);
printf("result = %2d\n", result );
// Passing the address of a function
result = any_function(product,a, b);
printf("result = %2d\n", result );
printf("result = %2d\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);
}
int sum(int x, int y) {
return x + y;
}
int product(int x, int y) {
return x * y;
}
int difference(int x, int y) {
return x - y;
}
上面的代碼生成以下結(jié)果。
更多建議: