|
Question : Using qsort with a CArray
|
|
When I try to use qsort on an CArray
CArray DblArray; DblArray.SetSize(6);
DblArray[0] = 0.00; DblArray[1] = 1.00; DblArray[2] = 3.00; DblArray[3] = 2.00; DblArray[4] = 5.00; DblArray[5] = 4.00;
qsort(DblArray,DblArray.GetSize,sizeof(double),compare);
I get a compile error C2664: 'qsort' : cannot convert parameter 1 from 'CArray' to 'void *' I tried (void*)DblArray same error; I tried &DblArray it compiles but all i get are 0.0 values;
every thing works on a simple double DblArray[] type array Whats wrong?
here is the rest of my example
static int compare (const void *val1, const void *val2) { static int compare (const void *val1, const void *val2) { double FUZZ = .00000001;
double f1 = *(double*)val1; double f2 = *(double*)val2;
if ( fabs (f1 - f2) <= FUZZ ) return 0; else if ( f1 > f2 ) return 1;
return -1; }
|
|
Answer : Using qsort with a CArray
|
|
Like this:
qsort(DblArray.GetData(),DblArray.GetSize,sizeof(double),compare);
GetData() returns a pointer to the data in the array. Here's the definition in 'afxtempl.h':
AFX_INLINE const TYPE* CArray::GetData() const { return (const TYPE*)m_pData; }
|
|
|
|