[关闭]
@Wayne-Z 2017-10-02T08:21:29.000000Z 字数 2981 阅读 1137

Function Pointer

ssd6



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.

  1. int addValue(int a, int b) {
  2. return a+b;
  3. }
  4. 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.

  1. int a = 20;
  2. int b = 10;
  3. int ans;
  4. //1th method
  5. ans = addValue(a, b);
  6. //2nd method
  7. ans = (*pf)(a, b);
  8. //3nd method
  9. 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.

  1. 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.

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

Function Pointer Array

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

  1. int (*pfArray[4])(int, int);
  2. pfArray[0] = addValue;
  3. pfArray[1] = subValue;
  4. pfArray[2] = plusValue;
  5. pfArray[3] = divValue;
  6. int addValue(int a, int b) {
  7. printf("a + b = ");
  8. return a+b;
  9. }
  10. int subValue(int a, int b){
  11. printf("a - b = ");
  12. return a-b;
  13. }
  14. int plusValue(int a, int b){
  15. printf("a * b = ");
  16. return a*b;
  17. }
  18. int divValue(int a, int b){
  19. printf("a / b = ");
  20. return a/b;
  21. }

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

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

Conclusion

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

  1. #include<stdio.h>
  2. int addValue(int a, int b);
  3. int subValue(int a, int b);
  4. int plusValue(int a, int b);
  5. int divValue(int a, int b);
  6. void operation(int a, int b, int(*op)(int, int));
  7. int main(){
  8. int (*pf)(int a, int b) = &addValue;
  9. int a = 20;
  10. int b = 10;
  11. int ans;
  12. //Show the use of function pointer
  13. //1th method
  14. printf("call addValue(10, 20):\n");
  15. ans = addValue(a, b);
  16. printf("%d \n", ans);
  17. //2nd method
  18. printf("call (*pf)(10, 20):\n");
  19. ans = (*pf)(a, b);
  20. printf("%d \n", ans);
  21. //3nd method
  22. printf("call (*pf)(10, 20): \n");
  23. ans = pf(a, b);
  24. printf("%d \n", ans);
  25. //Show callback function
  26. printf("Call back function and function pointer array used:\n");
  27. int (*pfArray[4])(int, int);
  28. pfArray[0] = addValue;
  29. pfArray[1] = subValue;
  30. pfArray[2] = plusValue;
  31. pfArray[3] = divValue;
  32. for (int i = 0; i < 4; i++){
  33. operation(a, b, pfArray[i]);
  34. }
  35. return 0;
  36. }
  37. int addValue(int a, int b) {
  38. printf("a + b = ");
  39. return a+b;
  40. }
  41. int subValue(int a, int b){
  42. printf("a - b = ");
  43. return a-b;
  44. }
  45. int plusValue(int a, int b){
  46. printf("a * b = ");
  47. return a*b;
  48. }
  49. int divValue(int a, int b){
  50. printf("a / b = ");
  51. return a/b;
  52. }
  53. void operation(int a, int b, int(*op)(int, int)){
  54. int ans = op(a,b);
  55. printf("%d \n", ans);
  56. }

Reference

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注