I have a struct declared like this
struct data
{
char * Date;
char * String;
};
struct data **RegArray = NULL;
int ArrayCount = 0;
I add new items to the array this way:
struct data **tmp = ( struct data ** )realloc( RegArray, ( ArrayCount + 1 ) * sizeof( struct data * ) );
if ( tmp == NULL )
{
printf( "\nRealloc failed!" );
return;
}
RegArray = tmp;
RegArray[ ArrayCount ] = ( struct data * )malloc( sizeof **RegArray );
if ( RegArray[ ArrayCount ] == NULL )
{
printf( "\nMalloc failed!" );
return;
}
RegArray[ ArrayCount ]->Date = _strdup( cDate );
RegArray[ ArrayCount ]->String = _strdup( cString );
ArrayCount++;
The function which compares the values:
int CompareByDate( const void *elem1, const void *elem2 )
{
//return ( ( data* )elem1 )->Date > ( ( data* )elem2 )->Date ? 1 : -1;
return strcmp( ( ( data* )elem1 )->Date, ( ( data* )elem2 )->Date );
}//CompareByDate
And finally I call qsort like this:
qsort( RegArray, ArrayCount-1, sizeof( data ), CompareByDate );
The problem is, that the data won't be sorted. So what am I doing wrong?
Thanks!