home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
c_all592.arj
/
TI659.ASC
< prev
next >
Wrap
Text File
|
1992-02-25
|
1KB
|
67 lines
PRODUCT : C++ NUMBER : 659
VERSION : All
OS : PC DOS
DATE : February 25, 1992 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 <stdlib.h>
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()