Qsort Pointer To Arrays Of Structs

Reply

Join Date: Dec 2007
Posts: 2
Reputation: stolson is an unknown quantity at this point 
Solved Threads: 0
stolson stolson is offline Offline
Newbie Poster

Qsort Pointer To Arrays Of Structs

 
0
  #1
Dec 11th, 2007
I have read in a file, and i can print it out successfully, but i must sort the pointer to the array of structures. below is my code. I must sort the phone book in ascending order by zip code. If anyone could please help that would be great.
  1. 1 #include <stdio.h>
  2. 2 #include <string.h>
  3. 3 #include <stdlib.h>
  4. 4
  5. 5 typedef struct{
  6. 6 char name[50];
  7. 7 char street[50];
  8. 8 char city_state[50];
  9. 9 int zip;
  10. 10 } ADDRESS;
  11. 11
  12. 12 void sort(ADDRESS *a, int * i);
  13. 13 ADDRESS* readin(ADDRESS *a, int * i, char *pathin);
  14. 14 void print(ADDRESS *a, int * i, char *pathout);
  15. 15 int compare(const void *p1, const void *p2);
  16. 16 int main(int argc, char * argv[])
  17. 17 {
  18. 18 int *i;
  19. 19
  20. 20 if(argc == 3){
  21. 21 ADDRESS*a = readin(a, i, argv[1]);
  22. 22 sort(a,i);
  23. 23 print(a, i, argv[2]);
  24. 24 }else{
  25. 25 fprintf(stderr, "Usage: %s <inputfilename> <outputfilename>\n",
  26. 26 argv[0]);
  27. 27 exit(1);
  28. 28 }
  29. 29
  30. 30 return 0;
  31. 31 }
  32. 32 ADDRESS *readin (ADDRESS *a, int *i,char *pathin)
  33. 33 {
  34. 34 char buffer[10000];
  35. 35 char temp_zip[50];
  36. 36 char *line;
  37. 37 int count=0;
  38. 38 FILE *fp;
  39. 39 fp = fopen(pathin, "r");
  40. 40 *i = 0;
  41. 41 a = (ADDRESS *)malloc(sizeof(ADDRESS)*50);
  42. 42
  43. 43
  44. 44 while (!feof(fp) && (*i < 49))
  45. 45 {
  46. 46
  47. 47 line = fgets(buffer, 1000, fp);
  48. 48 if(line == NULL)
  49. :set number 1,1 Top
  50. 92 fputs("\n", fq);
  51. 93 c++;
  52. 94 a++;
  53. 95 }
  54. 96 fclose(fq);
  55. 97 }
  56. 98 void sort(ADDRESS *a, int *i)
  57. 99 {
  58. 100 qsort(a, *i, sizeof(ADDRESS),compare);
  59. 101 }
  60. 102
  61. 103
  62. 104 int compare(const void *p1, const void *p2){
  63. 105 const struct ADDRESS *a1 = p1;
  64. 106 const struct ADDRESS *a2 = p2;
  65. 107 if(a1->zip > a2->zip){
  66. 108 return 1;
  67. 109 }else{
  68. 110 return -1;
  69. 111 }
  70. 112 return 0;
  71. 113 }
Last edited by Narue; Dec 11th, 2007 at 12:52 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: AndrewWood is an unknown quantity at this point 
Solved Threads: 0
AndrewWood AndrewWood is offline Offline
Newbie Poster

Re: Qsort Pointer To Arrays Of Structs

 
0
  #2
Dec 11th, 2007
Try this

  1. int compare(const void *p1, const void *p2)
  2. {
  3. const struct ADDRESS *a1 = ( const struct ADDRESS * ) p1 ;
  4. const struct ADDRESS *a2 = ( const struct ADDRESS * ) p2 ;
  5.  
  6. if ( a1 -> zip < a2 -> zip)
  7. return -1 ;
  8.  
  9. else if ( *a1 == *a2 )
  10. return 0 ;
  11.  
  12. else
  13. return 1 ;
  14. }
Last edited by AndrewWood; Dec 11th, 2007 at 6:23 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: stolson is an unknown quantity at this point 
Solved Threads: 0
stolson stolson is offline Offline
Newbie Poster

Re: Qsort Pointer To Arrays Of Structs

 
0
  #3
Dec 11th, 2007
Thank you, i found that i had to change a few things, notably i had to change my typedef struct to this:


  1. typedef [COLOR="Red"]struct pb_s[/COLOR]{
  2. char name[50];
  3. char street[50];
  4. char city_state[50];
  5. int zip;
  6. } ADDRESS;
  7.  
  8. and then:
  9.  
  10. void sort(ADDRESS *a, int *i)
  11. {
  12. qsort(a, *i, sizeof(struct pb_s),compare);
  13. }
  14.  
  15.  
  16. int compare(const void *p1, const void *p2){
  17. const struct pb_s *a1 = p1;
  18. const struct pb_s *a2 = p2;
  19. if(a1->zip < a2->zip){
  20. return -1;
  21. }else if(a1-> zip == a2->zip){
  22. return 0;
  23. }else{
  24. return 1;
  25. }
  26. }


making that pb_s at the top of the struct definition is what made it possible to work.
Last edited by WolfPack; Dec 11th, 2007 at 10:24 am. Reason: Added [CODE=CPP][/CODE] Tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: AndrewWood is an unknown quantity at this point 
Solved Threads: 0
AndrewWood AndrewWood is offline Offline
Newbie Poster

Re: Qsort Pointer To Arrays Of Structs

 
0
  #4
Dec 11th, 2007
Good I'm glad you got the problem...sorted, har har.

Not sure if your compiler supports it but another thing you can do is

struct ADDRESS {
char name[50];
char street[50];
char city_state[50];
int zip;
} ;

if you are going to use it locally

If you want to share it between modules you can put it in a header file as

volatile struct ADDRESS {
} address ;

so on & so forth
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC