C language function pointer as function parameter (callback function)

  • Use the callback function to print any type of data

    • Implement printing of int type data

//Provide a printing function that can print any type of data

//Give the printing requirements to the user
void myPrintInt(void *data){
int *p = data;
printf("%d\
", *p);
}

void printText(void *data, void(*myPrint)(void *)){
myPrint(data);
}

//Print int type data
void test01(){
int a = 10;
printText(& amp;a, myPrintInt);
}
  • Implement printing structure type data

//Structure type
struct Person{
char name[1024];
int age;
};

void printText(void *data, void(*myPrint)(void *)){
myPrint(data);
}
//User-written
void myPrintStruct(void *data){
struct Person *p = data;
printf("name = %s age = %d\
", p->name, p->age);
}
//print structure
void test02(){
struct Person person = {
"Bob", 10
};
printText(&person, myPrintStruct);
}
  • Use the callback function to print any type of array

    • Implement printing of int array

void printArray(void *nums,int eleSize, int len, void(*myPrint)(void *)){
char *p = nums;
int i = 0;
for(i = 0; i < len; i + + ){
char *eleAddr = p + eleSize * i;//Find the starting address of each element
\t
//Return the printing operation to the user
//printf("%d\
", nums[i]);
        myPrint(eleAddr);

}
}
//User-defined printing operation
void myPrintIntArray(void *data){
int *p = data;
printf("%d\
", *p);
}

void test03(){
int nums[] = {1,2,3,4,5};
int len = sizeof(nums) / sizeof(int);
printArray(nums,sizeof(int), len, myPrintIntArray);
}
  • Implement printing structure type array

//Structure type
struct Person{
char name[1024];
int age;
};
//Function to print an array of any type
void printArray(void *nums,int eleSize, int len, void(*myPrint)(void *)){
char *p = nums;
int i = 0;
for(i = 0; i < len; i + + ){
char *eleAddr = p + eleSize * i;//Find the starting address of each element
\t\t
//Return the printing operation to the user
//printf("%d\
", nums[i]);
        myPrint(eleAddr);
}
}
//User-defined printing operation
void myPrintStructArr(void *data){
struct Person *p = data;
printf("%s, %d\
", p->name, p->age);
}
    
void test04(){
struct Person person[] = {
{"aaa", 10},
{"bbb", 20},
{"ccc", 30},
{"ddd", 40},
};
int len = sizeof(person) / sizeof(struct Person);
printArray(person, sizeof(struct Person), len, myPrintStructArr);
}

  • Implementing the function of finding structure types

//Structure type
struct Person{
char name[1024];
int age;
};
//Implement search function
void findEle(void *pArray, void *data, int eleSize, int len, int(*compare)(void *, void *)){
char *p = pArray;
int i = 0;
int ret = -1;
for(i = 0; i < len; i + + ){
char *eleAddr = p + eleSize * i;//Find the starting address of each element
//Find the element
if(compare(eleAddr, data) == 0){
ret = 0;
}
}
if(ret==0){
printf("The array contains this element!");
}else{
printf("The element is not included in the array!");
}
\t
}
//Compare two elements, the same is 0, the difference is -1
int compare(void *data1, void *data2){
struct Person *p1 = data1;
struct Person *p2 = data2;
if(strcmp(p1->name, p2->name) == 0 & amp; & amp; p1->age == p2->age){
return 0;
}
return -1;
}
void test05(){
struct Person person[] = {
{"aaa", 10},
{"bbb", 20},
{"ccc", 30},
{"ddd", 40},
};
struct Person p = {
"ccc", 30
};
int len = sizeof(person) / sizeof(struct Person);
findEle(person, & amp;p, sizeof(struct Person), len, compare);
}
  • Provide a function to sort arrays of any type. The sorting rules use selection sorting. The sorting order is specified by the user

    • Sort array of int type

//Given by the user, determine the sort order
int sortRule01 (void *data1, void *data2){
int *p1 = data1;
int *p2 = data2;
\t
if(*p1 > *p2){
return 1;
} else if(*p1 < *p2){
return -1;
}else{
return 0;
}
}
//Achieve various types of sorting functions, implemented with selection sorting
void sortArray(void *pArray, int eleSize, int len, int(*sortRule)(void *, void *)){
char * temp = malloc(eleSize);
char *p = pArray;
int i = 0, j = 0;
for(i = 0; i < len; i + + ){
int minOrMax = i;
for(j = i + 1; j < len; j + + ){
//pArray[minOrMax] > pArray[j] 1
//pArray[minOrMax] < pArray[j] -1
//pArray[minOrMax] = pArray[j] 0
char *minOrMaxEle = p + eleSize * minOrMax;//Find the starting address of the largest or smallest element
char *curEle = p + eleSize * j;//Find the starting address of the current element
if(sortRule(minOrMaxEle, curEle) == 1){
minOrMax= j;
}
}
        //If the maximum or minimum value changes, then it needs to be exchanged and the maximum or minimum value is placed in front
if(minOrMax != i){
\t\t\t
char *indexMinOrMaxEle = p + eleSize * minOrMax;
char *indexIEle = p + eleSize * i;
\t\t\t
memcpy(temp, indexMinOrMaxEle, eleSize);
memcpy(indexMinOrMaxEle, indexIEle, eleSize);
memcpy(indexIEle, temp, eleSize);
}
}
if (temp != NULL)
{
free(temp);
temp = NULL;
}
}
//User-defined implementation of printing function
void myPrint01(void *data){
int *p = data;
printf("%d ", *p);
}
//Realize printing of various data types
void display(void *pArray,int eleSize, int len, void(*myPrint)(void *)){
char *p = pArray;
int i = 0;
for(i = 0; i < len; i + + ){
char *eleAddr = p + eleSize * i;
myPrint(eleAddr);
//printf("%d ", nums[i]);
}
}

void test06(){
int nums[] = {6,8,4,10,1};
int len = sizeof(nums) / sizeof(int);
sortArray(nums, sizeof(int), len, sortRule01);
display(nums,sizeof(int), len, myPrint01);
}
  • Sort structure types

    //Structure types
    struct Person{
    char name[1024];
    int age;
    };
    //Achieve various types of sorting functions, implemented with selection sorting
    void sortArray(void *pArray, int eleSize, int len, int(*sortRule)(void *, void *)){
    char * temp = malloc(eleSize);
    char *p = pArray;
    int i = 0, j = 0;
    for(i = 0; i < len; i + + ){
    int minOrMax = i;
    for(j = i + 1; j < len; j + + ){
    //pArray[minOrMax] > pArray[j] 1
    //pArray[minOrMax] < pArray[j] -1
    //pArray[minOrMax] = pArray[j] 0
    char *minOrMaxEle = p + eleSize * minOrMax;//Find the starting address of the largest or smallest element
    char *curEle = p + eleSize * j;//Find the starting address of the current element
    if(sortRule(minOrMaxEle, curEle) == 1){
    minOrMax= j;
    }
    }
            //If the maximum or minimum value changes, then it needs to be exchanged and the maximum or minimum value is placed in front
    if(minOrMax != i){
    \t\t\t
    char *indexMinOrMaxEle = p + eleSize * minOrMax;//Get the starting address of the maximum or minimum value of the original mark
    char *indexIEle = p + eleSize * i;//Get the real maximum or minimum address
    //Exchange
    memcpy(temp, indexMinOrMaxEle, eleSize);
    memcpy(indexMinOrMaxEle, indexIEle, eleSize);
    memcpy(indexIEle, temp, eleSize);
    }
    }
    if (temp != NULL)
    {
    free(temp);
    temp = NULL;
    }
    }
    //Realize printing of various data types
    void display(void *pArray,int eleSize, int len, void(*myPrint)(void *)){
    char *p = pArray;
    int i = 0;
    for(i = 0; i < len; i + + ){
    char *eleAddr = p + eleSize * i;
    myPrint(eleAddr);
    //printf("%d ", nums[i]);
    }
    }
    //User-defined printing operation
    void myPrint02(void *data){
    struct Person *p = data;
    printf("name = %s age = %d\
    ", p->name, p->age);
    }
    //User-defined sorting operation, sorting based on age size
    int sortRule02 (void *data1, void *data2){
    struct Person *p1 = data1;
    struct Person *p2 = data2;
    \t
    if(p1->age > p2->age){
    return 1;
    } else if(p1->age < p2->age){
    return -1;
    }else{
    return 0;
    }
    }
    void test07(){
    struct Person person1[] = {
    {"aaa", 6},
    {"bbb", 8},
    {"ccc", 4},
    {"ddd", 10},
    {"eeee", 1}
    };
    int len = sizeof(person1) / sizeof(struct Person);
    sortArray(person1, sizeof(struct Person), len, sortRule02);
    display(person1, sizeof(struct Person), len, myPrint02);
    }