1. Array Pointer Pointer function pointer 2. 3. 4. The array of function pointers pointing to the array of function pointers

1. appreciated that the array pointer
can be referred to as a pointer to an array Array Pointer
Pointer Array FIG
Here Insert Picture Description
array pointer in FIG.
Here Insert Picture Description
2 and defined in understanding the function pointer
First, it is a pointer to the address of the function.
Must specify the function pointer defining a function pointer and the return value of the function parameter list pointed. (Brackets essential, to match type)
3. understood and defined array of function pointers, transfer table
First, it is an array, the array elements are a function of the address, i.e. the address of the function stored in the array
int ( pfun [2]) ( int, int) = {Add, Sub};
use of function pointers ---- transition table, implemented by transfer of a simple calculator table
4. understanding pointers and function pointers define an array of
function pointers array and the pointer points to an array element of the array of function pointers are
int (
(P *) [2]) (int, int) = & pfun;

char* test1(int a,int* b)
{
}
char* test1(int a,int* b)
{
}
int main()
{
  char* (*p1)(int,int*)=test1;`//函数指针
  char* (*p2[2])(int,int*)={test1,test2};//函数指针数组
  char*(*(*p3[2])(int,int*)=&p2; //指向函数指针数组的指针
}

5. understand the use of the callback function of
the callback function is the function that is called through a function pointer
in the C language callback function
callback structure mainly composed of three parts: the main function, and the function calls the called function. C, the called function generally function pointer (pointing to the corresponding function entry address) of the form

//定义回调函数
void PrintfText() 
{
    printf("Hello World!\n");
}
//定义实现回调函数的"调用函数"
// 参数为函数指针,无参数返回void
void CallPrintfText(void (*callfuct)())
{
    callfuct();
}
//实现函数回调
int main(int argc,char* argv[])
{
    CallPrintfText(PrintfText);
    return 0;
}

6. Various types of exercise data sorting function qsort

void qsort(void *base, size num,size_t width,int(*cmp)(const void *e1,const void *e2))
//base:要排序的数组
//num:数组中的元素数目
//size_t width:宽度
//cmp:比较两个数组元素的比较函数

7. imitate qsort function to achieve a common bubble sort

void bubble_t(int* arr,int sz)
{
    int i=0;
    for(i=0; i<sz-1; i++)
    {
       int j=0;
       for(j=0; j<sz-1-i; j++)
         { 
            if (arr[j]>arr[j+1])
            {
               int tmp=arr[j];
               arr[j]=arr[j+1];
               arr[j+1]=tmp;
             }
          }
     }
 }
void print_arr(int arr[],int sz)
{
    int i=0;
    for(i=0;i<sz;i++)
    {
        printf("%d ",arr[i]);
    }
    printf("\n");
 }
int main()
{
    int arr[]={9,8,7,6,5,4,3,2,1,0};
    int sz=sizeof(arr)/sizeof(arr[0]);
    bubble_sort(arr,sz);
    print_arr(arr,sz);
    return 0;
}
Published 60 original articles · won praise 23 · views 3335

Guess you like

Origin blog.csdn.net/weixin_44945537/article/details/94362812