سؤال

I have this part, to begin with (I'll be using it to create employee structures):

    typedef struct 
    { 
       char first_name[20], last_name[20]; 
       int birthdate, temporary; 
       //date of birth YEAR/MONTH/DAY
       //temporary employees: 1, else 0

    }factory;

I will be reading the data using the following function:

    void reading(factory *employee,int *nr) 
    { 
      ++(*nr); 
      printf("First name:\n");
      fflush(stdin); 
      gets((employee+*nr)->first_name);

      printf("Last name:\n");
      fflush(stdin); 
      gets((employee+*nr)->last_name);

      printf("Birthdate:\n");
      fflush(stdin);
      scanf ("%d" , &((employee + *nr)->birthdate));

      printf("Temporary employee? 1 for YES, 0 for NO");
      fflush(stdin);
      scanf("%d", &((employee + *nr)->temporary));

    }

And the comparison function (I don't think it's properly written, any suggestions on how to modify it would be great -- should it be "factory employee *ia" instead of struct?):

int struct_cmp_by_name(const void *a, const void *b)
{
struct employee *ia = (struct employee *)a;
struct employee *ib = (struct employee *)b;
return strcmp(ia->last_name, ib->last_name);

}

I also have a simple display function whose prototype I'll copy below:

void display(factory *employee, int nr) 

Other portions of code have been omitted for the sake of space. How would I implement the qsort function in this case? I have the comparison function, but I don't know what the base array should be or how to find the other two size parameters. Anticipated thanks for any help provided.

هل كانت مفيدة؟

المحلول

It should be

factory *ia = (factory *)a;

or you could skip the assignments and just cast

... ((factory *)a)->last_name ...

I assume somewhere in your code you allocate or declare space for an array of factory?

For the quick sort, you can either swap the structures, or you can create an array of pointers to structures and swap pointers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top