/*函数指针*/ #include<stdio.h> void PrintMin(int a, int b) { if (a < b) printf("%d\n", a); else printf("%d\n", b); } int main() { void(*pf)(int, int); int x = 6, y = 5; //PrintMin(x, y); pf = PrintMin; pf(x, y); //while (1); system("PAUSE"); return 0; } //void qsort(void *base, int nelem, unsigned int width, // int(*pfCompare)(const void *, const void *)); //是 void * 类型 //int 比较函数名(const void * elem1, const void * elem2); //比较函数编写规则: //1) 如果 * elem1应该排在 * elem2前面,则函数返回值是负整数 //2) 如果 * elem1和* elem2哪个排在前面都行,那么函数返回0 //3) 如果 * elem1应该排在 * elem2后面,则函数返回值是正整数 #include <stdio.h> #include<stdlib.h> int MyCompare(const void* elem1, const void* elem2) { unsigned int *p1, *p2; p1 = (unsigned int*)elem1; // *elem1 非法,因为不知道数据类型 p2 = (unsigned int*)elem2; //return (*p1 % 10) - (*p2 % 10) ; return (*p2 % 10) - (*p1 % 10); //return 0; } #define NUM 5 int main() { unsigned int an[NUM] = { 8,123,11,10,4 }; qsort(an, NUM, sizeof(unsigned int), MyCompare); for (int i = 0; i < NUM; i++) printf("%d ", an[i]); while (1); return 0; }
《C++程序设计POJ》《WEEK1 从C走进C++》函数指针
原文:https://www.cnblogs.com/focus-z/p/10923887.html