here i'm again. I had a problem while i was trying to sort an array of struct in c, i know that my code is not good enough but don't be rude with me please!
I tried changing function parameters but i'm exhausted and i'm sure that i'm going to do more errors if i continue, so i need your help.
Here the full code of my program https://pastebin.com/p28EbY8i
// I've 2 struct
typedef struct{ // Not used in this function
int id;
char * nome;
char * presidente;
char * allenatore;
} squadra;
typedef struct{ // I've an array of this type of data
int id;
char * nome;
char * cognome;
int eta;
char * ruolo;
squadra team;
char * college;
int td;
} giocatore;
// This is what i wrote for my function
size_t ordina_classifica(size_t sz, giocatore array[]){ //sz is the array
//size
giocatore temp;
for(size_t i = 0; i < sz; ++i){
for(size_t j = i + 1; j < sz; ++j){
if(array[i].td > array[j].td){
temp.id = array[i].id;
temp.nome = array[i].nome;
temp.cognome = array[i].cognome;
temp.eta = array[i].eta;
temp.ruolo = array[i].ruolo;
temp.team.nome = array[i].team.nome;
temp.college = array[i].college;
temp.td = array[i].td;
array[i].id = array[j].id;
array[i].nome = array[j].nome;
array[i].cognome = array[j].cognome;
array[i].eta = array[j].eta;
array[i].ruolo = array[j].ruolo;
array[i].team.nome = array[j].team.nome;
array[i].college = array[j].college;
array[i].td = array[j].td;
array[j].id = temp.id;
array[j].nome = temp.nome;
array[j].cognome = temp.cognome;
array[j].eta = temp.eta;
array[j].ruolo = temp.ruolo;
array[j].team.nome = temp.team.nome;
array[j].college = temp.college;
array[j].td = temp.td;
}
}
}
return 2; // I need this for the rest of the code (in case of sort
// success)
}
//I need to sort my array by the data 'td' that is a integer but for now my //compiler don't print errors, but only crash when i try to select this //function.
qsort?ordina_classifica()is called. "sz is the array size" hint that the calling code may be in error. See minimal reproducible example