Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. struct clients {
  2. int id;
  3. char name[27];
  4. };
  5.  
  6. int comparator(const void* p, const void* q){
  7. return strcmp(((struct list_node*)p)->name,
  8. ((struct list_node*)q)->name);
  9. }
  10.  
  11. void sort_by_name(struct list_node *node){
  12.  
  13. const int length = count_list(node);
  14. struct list_node *arr[length];
  15. int i=0;
  16. while (node)
  17. {
  18. arr[i]=node;
  19. node = node->next;
  20. i++;
  21. }
  22.  
  23. printf("Before sort:n");
  24. for (i = 0; i < length; i++) {
  25. printf("Id = %d, Name = %sn",
  26. arr[i]->id, arr[i]->name);
  27. }
  28.  
  29. qsort(arr, length, sizeof(struct list_node), comparator);
  30.  
  31. printf("After sort:n");
  32. for (i = 0; i < length; i++) {
  33. printf("Id = %d, Name = %sn",
  34. arr[i]->id, arr[i]->name);
  35. }
  36.  
  37. }
  38.  
  39. Before sort:
  40. Id = 11, Name = adam
  41. Id = 20, Name = mati
  42. Id = 25, Name = zenek
  43. Id = 28, Name = mat
  44.  
  45. Process finished with exit code -1073741819 (0xC0000005).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement