In the previous article, we discussed the declaration and definition of pointers and other basic concepts. However, our understanding of pointers is still insufficient, so in this lesson, we will continue to learn about pointers. To reinforce our understanding, let’s first look at a classic pointer example code. Can you find the correct answer?Question: Now there are two integer variables a and b. I need to swap the actual values in memory corresponding to these two variables. Which of the following codes is correct? Please choose:A.
#include<stdio.h>void swap(int a,int b){int c=a; a=b; b=c;}int main(){int a=1,b=2;swap(a,b);printf("a=%d,b=%d",a,b);return 0;}
B.
#include<stdio.h>void swap(int a,int b){ int temp=a; a=b; b=temp;}int main(){int a=1,b=2;swap(&a,&b);printf("a=%d,b=%d",a,b);return 0;}
C.
#include <stdio.h>void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp;}int main() { int a = 1, b = 2; swap(&a, &b); printf("a=%d, b=%d", a, b); return 0;}
Think about this question, try typing each code on your own computer, see the results, and try to understand the logic of the code. If you have better ideas, feel free to discuss them in the comments. Now, let’s officially start our learning on function pointers and callback functions.Function PointersA function pointer is a pointer variable that points to a function.Typically, when we talk about pointer variables, we refer to pointers that point to integer, character, or array variables, while a function pointer points to a function. Function pointers can be used to call functions and pass parameters just like regular functions. The declaration of a function pointer type is as follows:
return_type (*pointer_name)(parameter_type1, parameter_type2); // Declare a function pointer type that points to a function with the same parameters and return type
Example
#include <stdio.h>int max(int x, int y){ return x > y ? x : y;}int (*p)(int,int); int main(void){ /* p is a function pointer */ p = &max; // & can be omitted int a, b, c, d; printf("Please enter three numbers:"); scanf("%d %d %d", &a, &b, &c); /* Equivalent to directly calling the function, d = max(max(a, b), c) */ d = p(p(a, b), c); printf("The largest number is: %d\n", d); return 0;}
The compilation and execution results are as follows:
Callback FunctionsA function pointer can be used as a parameter for another function.A function pointer variable can be used as a parameter for a function, and a callback function is a function that is called through a function pointer.In simple terms: a callback function has one or more parameters of function type, rather than basic data types in C language.Example
#include <stdio.h>int max(int x, int y){ return x > y ? x : y;}int printValue(int (*p)(int,int)){ printf("%d",p(2,10));}int main(void){ printValue(max); return 0;}
The final compilation and execution results are as follows:
This is actually similar to the previous example. Think about it more, and if you have any questions, feel free to discuss them in the comments. Here is a vivid example from the Zhihu author Chang Xiling:“You go to a store to buy something, but the item you want is out of stock. So you leave your phone number with the clerk. A few days later, when the item is back in stock, the clerk calls your number, and when you receive the call, you go to the store to pick up the item. In this example, your phone number is the callback function, leaving your number with the clerk is registering the callback function, the store having the item back in stock is the event that triggers the callback, the clerk calling you is calling the callback function, and you going to the store to pick up the item is responding to the callback event.”Well, that concludes our discussion on function pointers and callback functions today. In daily programming, they may not be used frequently, but understanding their execution process is an indispensable part of learning programming. In the next lesson, we will learn about strings in C language and experience the usage scenarios of strings in programming (づ。◕‿‿◕。)づ