函数指针


Concepts

Function pointer is a kind of pointer whicn points to function. There two commom uses of function pointers, jump tables and passing a function pointer as an argument in a function call.

Uses&Examples

The pointer to function must be initialized to point to something before indirection can be performed on it, which means, to use function pointers, we should firstly have a function already declared, and then we can assign a pointer that fits this function to it.

int addValue(int a, int b) {
    return a+b;
}
int (*pf)(int a, int b) = &addValue;

As we can see, the second declaration create pf, a pointer to function, with two arguments, making its type the same as function addValue. Thus we this initialization is avaliable. To use the function pointer, we have three ways.

int a = 20;
int b = 10;
int ans;
//1th method
ans = addValue(a, b);
//2nd method
ans = (*pf)(a, b);
//3nd method
ans = pf(a, b);

The outcome was shown in pf1.c

Callback Functions

The more important way of function pointer is callback function. we can implement an operation function, and we pass a function pointer to the operation, then we can decide indeed which operation is used when we call operation.

void operation(int a, int b, int(*op)(int, int));

we pass an function pointer to the operation function, and we can use the function pointer paseed in the block.

void operation(int a, int b, int(*op)(int, int)){
    int ans = op(a,b);
    printf("%d \n", ans);
}

Function Pointer Array

This time, we declare an array of function pointers, and initialize each of them

int (*pfArray[4])(int, int);
pfArray[0] = addValue;
pfArray[1] = subValue;
pfArray[2] = plusValue;
pfArray[3] = divValue;

int addValue(int a, int b) {
    printf("a + b = ");
    return a+b;
}
int subValue(int a, int b){
    printf("a - b = ");
    return a-b;
}
int plusValue(int a, int b){
    printf("a * b = ");
    return a*b;
}
int divValue(int a, int b){
    printf("a / b = ");
    return a/b;
}

And we can call them each time by just access each item of the array.

for (int i = 0; i < 4; i++){
     operation(a, b, pfArray[i]);
}

Conclusion

From all above, we have had a basic knowledge of function pointers, and the executable code used in this report are as follows.

#include<stdio.h>

int addValue(int a, int b);
int subValue(int a, int b);
int plusValue(int a, int b);
int divValue(int a, int b);
void operation(int a, int b, int(*op)(int, int));

int main(){
    int (*pf)(int a, int b) = &addValue;
    int a = 20;
    int b = 10;
    int ans;

    //Show the use of function pointer
    //1th method
    printf("call addValue(10, 20):\n");
    ans = addValue(a, b);
    printf("%d \n", ans);
    //2nd method
    printf("call (*pf)(10, 20):\n");
    ans = (*pf)(a, b);
    printf("%d \n", ans);
    //3nd method    
    printf("call (*pf)(10, 20): \n");
    ans = pf(a, b);
    printf("%d \n", ans);
    //Show callback function
    printf("Call back function and function pointer array used:\n");
    int (*pfArray[4])(int, int);
    pfArray[0] = addValue;
    pfArray[1] = subValue;
    pfArray[2] = plusValue;
    pfArray[3] = divValue;
    for (int i = 0; i < 4; i++){
        operation(a, b, pfArray[i]);
    }
    return 0;
}

int addValue(int a, int b) {
    printf("a + b = ");
    return a+b;
}
int subValue(int a, int b){
    printf("a - b = ");
    return a-b;
}
int plusValue(int a, int b){
    printf("a * b = ");
    return a*b;
}
int divValue(int a, int b){
    printf("a / b = ");
    return a/b;
}
void operation(int a, int b, int(*op)(int, int)){
    int ans = op(a,b);
    printf("%d \n", ans);
}

Reference

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,131评论 0 23
  • 我还是会哭。还是会想。 每个存在过的人都是生命的依靠。如果一个人没了,那根柱子就塌了。但是偏偏一地残骸提醒着之前的...
    王汪汪汪汪阅读 244评论 0 0
  • 那天我想了一万种不同的措辞,究竟怎样听起来才会让她觉得心疼。 「我不想闹啦!能不能问你最后一个问题如果抛开我的主观...
    生活脱轨阅读 210评论 0 0
  • 2015年11月26日,西洋感恩节。 我们好像越来越从心底里接收西方的节日,而且每次都过得比自己本来的节日欢庆。或...
    晶莹剔透阅读 941评论 0 0