B
Bidule
Hi,
I'm trying to sort structs defined as follows:
struct combinationRec {
float score;
char* name;
};
The number of structs and the length of the "name" field are not known
at compile time.
I've been struggling with this code all afternoon...
I believe I'm sticking to what's explained into the C faq. This is
probably not the case because qsort just crashes on me!
Does anyone have an idea why ?
Matt
/*
* qsort of malloc'ed structs
*/
#include <stdio.h>
#include <stdlib.h>
struct combinationRec {
float score;
char* name;
};
// Score sorting function
int scoreSort(const void *p1, const void *p2) {
struct combinationRec *sp1 = *(struct combinationRec * const *)p1;
struct combinationRec *sp2 = *(struct combinationRec * const *)p2;
if (sp1->score < sp2->score) return -1;
if (sp1->score > sp2->score) {return +1;}
else {return 0;}
}
int main(int argc, char* argv[])
{
combinationRec *allCombinations;
combinationRec *currComb;
int i, j;
int numStructs;
int nameLength;
char letter;
randomize();
numStructs = rand()%15+1;
if ( (allCombinations = (combinationRec *) malloc( sizeof
(combinationRec)*numStructs)) == NULL ) {
printf("Not enough memory to allocate buffer\n");
exit(EXIT_FAILURE);
}
for (i=0; i<numStructs; i++) {
currComb = &allCombinations;
nameLength = rand()%15+1;
if ( (currComb->name = (char *) malloc( sizeof (char)*(
nameLength + 1 ))) == NULL ) {
printf("Not enough memory to allocate buffer\n");
exit(EXIT_FAILURE);
}
currComb->score = (float) (rand()% 50) / 3.14159;
for (j=0; j<nameLength; j++) {
letter = rand()%26 + 'A';
currComb->name[j] = letter;
}
currComb->name[nameLength] = '\0';
}
// Display structs before sort
printf("Before sort:\n");
for (i=0; i<numStructs; i++) {
printf("struct no. %2i: score=%7.3f name=%s\n",
i,
allCombinations.score,
allCombinations.name
);
}
printf("Calling qsort\n");
// Sort structs by score
qsort(allCombinations, numStructs , sizeof( allCombinations[0] ),
scoreSort);
// Show sorted scores
printf("Sorted:\n");
for (i=0; i<numStructs; i++) {
printf("struct no. %2i: score=%7.3f name=%s\n",
i,
allCombinations.score,
allCombinations.name
);
}
// Cleanup
for (i=0; i<numStructs; ++i) {
free((void *)allCombinations.name);
}
free((void *)allCombinations);
// Exit
return 0;
}
I'm trying to sort structs defined as follows:
struct combinationRec {
float score;
char* name;
};
The number of structs and the length of the "name" field are not known
at compile time.
I've been struggling with this code all afternoon...
I believe I'm sticking to what's explained into the C faq. This is
probably not the case because qsort just crashes on me!
Does anyone have an idea why ?
Matt
/*
* qsort of malloc'ed structs
*/
#include <stdio.h>
#include <stdlib.h>
struct combinationRec {
float score;
char* name;
};
// Score sorting function
int scoreSort(const void *p1, const void *p2) {
struct combinationRec *sp1 = *(struct combinationRec * const *)p1;
struct combinationRec *sp2 = *(struct combinationRec * const *)p2;
if (sp1->score < sp2->score) return -1;
if (sp1->score > sp2->score) {return +1;}
else {return 0;}
}
int main(int argc, char* argv[])
{
combinationRec *allCombinations;
combinationRec *currComb;
int i, j;
int numStructs;
int nameLength;
char letter;
randomize();
numStructs = rand()%15+1;
if ( (allCombinations = (combinationRec *) malloc( sizeof
(combinationRec)*numStructs)) == NULL ) {
printf("Not enough memory to allocate buffer\n");
exit(EXIT_FAILURE);
}
for (i=0; i<numStructs; i++) {
currComb = &allCombinations;
nameLength = rand()%15+1;
if ( (currComb->name = (char *) malloc( sizeof (char)*(
nameLength + 1 ))) == NULL ) {
printf("Not enough memory to allocate buffer\n");
exit(EXIT_FAILURE);
}
currComb->score = (float) (rand()% 50) / 3.14159;
for (j=0; j<nameLength; j++) {
letter = rand()%26 + 'A';
currComb->name[j] = letter;
}
currComb->name[nameLength] = '\0';
}
// Display structs before sort
printf("Before sort:\n");
for (i=0; i<numStructs; i++) {
printf("struct no. %2i: score=%7.3f name=%s\n",
i,
allCombinations.score,
allCombinations.name
);
}
printf("Calling qsort\n");
// Sort structs by score
qsort(allCombinations, numStructs , sizeof( allCombinations[0] ),
scoreSort);
// Show sorted scores
printf("Sorted:\n");
for (i=0; i<numStructs; i++) {
printf("struct no. %2i: score=%7.3f name=%s\n",
i,
allCombinations.score,
allCombinations.name
);
}
// Cleanup
for (i=0; i<numStructs; ++i) {
free((void *)allCombinations.name);
}
free((void *)allCombinations);
// Exit
return 0;
}