PRODUCT : C++ NUMBER : 659 VERSION : All OS : PC DOS DATE : September 17, 1991 PAGE : 1/1 TITLE : An Example of Using qsort() in C++ Mode Due to the strict type checking enforced by the C++ language, one must insure that the types of the parameters to qsort match exactly. The types of the parameters passed to the compare function are the area for grief here. By using a typedef which typecasts the compare function to have the types required by the ANSI C prototype for qsort(), C++ can be made to accept our code. //--------------------------------------------------------------- // QSORTX.CPP - using qsort() in C++ mode #include typedef int (*cmp_func)(const void *, const void *); int compare( const int *one, const int *two ) { if (*one > *two) return -1 else return 1; } // end of compare() int a[3] = { 50, 10, 20 }; main() { qsort(a, 3, sizeof(a[0]), compare); // Does not compile in C++ qsort(a, 3, sizeof(a[0]), (cmp_func)compare); // Does compile } // end of main()