The day before yesterday, Peng Brother told us the callback function during class. Although he foggy a lot of crackling, I still listen to the words. (Not our Peng brother said Bad, O (∩_∩) o haha ~)
The most important thing is what the callback function is and what the callback function is. I don't know what it is, of course I can't write him. So I have a variety of Baidu, Google.
Next, I'll use my words to tell you what a callback function is.
What is a callback function (callback)
The callback function is a procedural concept. The essence is to ask someone else to do something and put in extra information.
function A calls function B
For example, A is called B to do things, when B in doing this thing, the need for their own information is not enough, and a has. You need a to come in from the outside, or B to take the initiative to apply outside. For B, a passive get message, an unsolicited message. Someone gives the terms in both ways, called the push of the message, and the pull of the information.
Let's give a vivid example.
you have a task, but a part of you will not do, or do not want to do, so I will help you to do this part, you do your other tasks work or wait for my message, but when I finished I want to inform you that I have done, you can use, I how to inform you? You give me a cell phone, let me finish and call you , I will call you, you get my results add to your work, continue to do other work. This is called callback, cell phone is I notify you of the means, it is called callback function, also known as callbacks function .
Simple use of callback functions
Use bubbling sorting to arrange strings, reshape
The functions are as follows:
In practice, it is often renamed for convenience
typedef int (*CMP) (const void *elem1, const void *ELEM2);
Swap functions:
Regardless of the type of row, we always exchange two elements when sorting, so we should have a swap function first.
void Swap (char *p1, char *p2, int size)//In this is in char, in the shaping interchange and the string Exchange when//both can be used {int i = 0;char tmp = 0;for (i = 0; i < size; i++) {tmp = * (p1 + i); * (P1 + i) = * (P2 + i); * (P2 + i) = tmp;}}
A callback function that compares integers:
int compare_int (const void *elem1, const void *ELEM2) {int a = * (int *) Elem1;int b = * (int *) Elem2;return a A;}
To compare the callback function of a string:
int compare_str (const void *elem1, const void *elem2) {Char a = * (char *) * (int *) Elem1;char b = * (char *) * (int *) Elem2;retu RN A-B;}
Bubble Sort:
Void bubble_sort (VOID&NBSP;*ARR,&NBSP;INT&NBSP;N,INT&NBSP;SIZE,CMP&NBSP;CMP) { ASSERT (arr); int i = 0; int j = 0; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (CMP ((char *) arr + j*size), ((char *) arr + (j + 1) *size)) > 0) { swap (( char *) arr + j*size), ((char *) arr + (j + 1) *size), size); } } }}
Main function:
int main () {char *str[] = {"BSJDFVGN", "aaaaaa", "FGHGJJ", "Mniig"};int arr[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};int i = 0;int n_int = sizeof (arr)/sizeof (arr[0]), int n_str = sizeof (str)/sizeof (str[0]); Bubble_sort (str, N_STR, sizeof (Str[0]) , compare_str);p rintf ("Bubble_sort str in:\n"); for (i = 0; i < n_str; i++) {printf ("%s", Str[i]);} printf ("\ n"); Bubble_sort (arr, n_int, sizeof (Arr[0]), Compare_int);p rintf ("Bubble_sort int in:\n"); for (i = 0; i < n_in T i++) {printf ("%d", Arr[i]);} System ("pause"); return 0;}
Note: The main idea: what elements are ordered to convert their coercion type to (char*), because Char can be treated as a base class. The forced type is then converted to the target type based on the type of the sorted element.
For example:
int compare_int (const void *elem1, const void *ELEM2) {int a = * (int *) Elem1;int b = * (int *) Elem2;return a A;}
Convert the base class coercion type to int* and then get the data of type int. The same is true for string comparison functions.
Bubble Sort callback Function!