| | |
Qsort Pointer To Arrays Of Structs
![]() |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
C Syntax (Toggle Plain Text)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 typedef struct{ 6 char name[50]; 7 char street[50]; 8 char city_state[50]; 9 int zip; 10 } ADDRESS; 11 12 void sort(ADDRESS *a, int * i); 13 ADDRESS* readin(ADDRESS *a, int * i, char *pathin); 14 void print(ADDRESS *a, int * i, char *pathout); 15 int compare(const void *p1, const void *p2); 16 int main(int argc, char * argv[]) 17 { 18 int *i; 19 20 if(argc == 3){ 21 ADDRESS*a = readin(a, i, argv[1]); 22 sort(a,i); 23 print(a, i, argv[2]); 24 }else{ 25 fprintf(stderr, "Usage: %s <inputfilename> <outputfilename>\n", 26 argv[0]); 27 exit(1); 28 } 29 30 return 0; 31 } 32 ADDRESS *readin (ADDRESS *a, int *i,char *pathin) 33 { 34 char buffer[10000]; 35 char temp_zip[50]; 36 char *line; 37 int count=0; 38 FILE *fp; 39 fp = fopen(pathin, "r"); 40 *i = 0; 41 a = (ADDRESS *)malloc(sizeof(ADDRESS)*50); 42 43 44 while (!feof(fp) && (*i < 49)) 45 { 46 47 line = fgets(buffer, 1000, fp); 48 if(line == NULL) :set number 1,1 Top 92 fputs("\n", fq); 93 c++; 94 a++; 95 } 96 fclose(fq); 97 } 98 void sort(ADDRESS *a, int *i) 99 { 100 qsort(a, *i, sizeof(ADDRESS),compare); 101 } 102 103 104 int compare(const void *p1, const void *p2){ 105 const struct ADDRESS *a1 = p1; 106 const struct ADDRESS *a2 = p2; 107 if(a1->zip > a2->zip){ 108 return 1; 109 }else{ 110 return -1; 111 } 112 return 0; 113 }
Last edited by Narue; Dec 11th, 2007 at 12:52 pm. Reason: Added code tags
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
Try this
C Syntax (Toggle Plain Text)
int compare(const void *p1, const void *p2) { const struct ADDRESS *a1 = ( const struct ADDRESS * ) p1 ; const struct ADDRESS *a2 = ( const struct ADDRESS * ) p2 ; if ( a1 -> zip < a2 -> zip) return -1 ; else if ( *a1 == *a2 ) return 0 ; else return 1 ; }
Last edited by AndrewWood; Dec 11th, 2007 at 6:23 am.
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Solved Threads: 0
Thank you, i found that i had to change a few things, notably i had to change my typedef struct to this:
making that pb_s at the top of the struct definition is what made it possible to work.
cpp Syntax (Toggle Plain Text)
typedef [COLOR="Red"]struct pb_s[/COLOR]{ char name[50]; char street[50]; char city_state[50]; int zip; } ADDRESS; and then: void sort(ADDRESS *a, int *i) { qsort(a, *i, sizeof(struct pb_s),compare); } int compare(const void *p1, const void *p2){ const struct pb_s *a1 = p1; const struct pb_s *a2 = p2; if(a1->zip < a2->zip){ return -1; }else if(a1-> zip == a2->zip){ return 0; }else{ return 1; } }
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
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
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
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
![]() |
Other Threads in the C Forum
- Previous Thread: Need a small help in a C program
- Next Thread: Sorting words
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking highest homework i/o inches include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft mqqueue mysql number odf open openwebfoundation owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming socketprogramming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h





