Category : C Source Code
Archive   : BCPPTECH.ZIP
Filename : TI659.ASC

 
Output of file : TI659.ASC contained in archive : BCPPTECH.ZIP







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

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()



















  3 Responses to “Category : C Source Code
Archive   : BCPPTECH.ZIP
Filename : TI659.ASC

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/