Hi everyone . I'm still a beginner in C especially with structures and pointers. So what i want to do here is to sort an Array of pointers to Structs based on a string inside the Struct . All the Element are dynamically allocated . And i want to know how to free the allocated Memory . My code is written in German so i will be writing only the parts that i don't understand in English . thank you in advance I appreciate your help.
and i will be uploading the whole Programme but it is written in German and there is a lot of non complete function in it .
Code:
typedef struct Vehicle { char *manufacturer; char *serialnumber; int weight; }; void SortByID(struct fahrzeug** data, int Anzahl){ struct Vehicle *temp= (struct Vehicle*)malloc( sizeof(struct Vehicle) ) ; int i =0 ; while ( i < Anzahl && strcmp(data[i]->serialnumber, data[i+1]->serialnumber) < 0) { temp = data[i+1]; data[i + 1] = person[i]; data[i]=temp ; i++; } } free(temp); temp=NULL; } int main(){ struct Vehicle **array = NULL ; array=(struct Vehicle**)malloc( 5 * sizeof(struct Vehicle*) ); SortByID ( &array, count ) ; return 0; }
Comment