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 #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 }

Try this

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 ;
 }

Thank you, i found that i had to change a few things, notably i had to change my typedef struct to this:

typedef struct pb_s{
        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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.