I'm trying to sort an array of structs in C - I've been trying to use qsort to do this, however, whenever the sorterFunction is called, I get a segmentation fault. I'm not exactly sure what I'm doing wrong here.
This is the struct I have populated my array with
typedef struct Song
{
char* title;
char* artist;
char* year;
} Song;
These are the sorting function
int comparisonFunction(const void *first, const void *second)
{
Song *songPtr = (Song *)first;
Song *songPtr2 = (Song *)second;
return strcmp(songPtr->title,songPtr2->title);
}
void sorterFunction(Song* songList, int globalCounter)
{
Song newGlobalList[1024];
// the following line is the one that causes segmentation fault
qsort(newGlobalList, globalCounter, sizeof(Song), comparisonFunction);
int count = 0;
while(count < globalCounter)
{
printf("%i Title: %s, Artist: %s, Year: %s\n",count+1,newGlobalList[count].title,newGlobalList[count].artist,newGlobalList[count].year);
count++;
}
}
newGlobalListarray? But do you even need the temporary array? Because with the structure you show thensongListcan't be a linked list, but must be a dynamically allocated array, which means you can use it directly.aandbin thecomparisonFunctionshould befirstandsecond.globalCountervalue? EachSongstructure insongListis properly initialized? Can you please try to create a Minimal, Complete, and Verifiable Example and show us?