qsort problem

This is a discussion on qsort problem within the C Programming forums, part of the General Programming Boards category; I was wondering if you can use the qsort function with an array of data structures in c? I have ...

  1. #1
    Unregistered
    Guest

    qsort problem

    I was wondering if you can use the qsort function with an array of data structures in c?

    I have the following code -
    qsort(arrayname,arraysize,sizeof(char),compare);

    int compare( const void *arg1, const void *arg2 )
    {
    int strcomp = strcmp(arg1, arg2);

    if (strcomp < 0)
    return -1;
    if (strcomp == 0)
    return 0;
    return 1;
    }

    I am trying to sort by a specific field in the structure (ie arrayname.firstname)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Of course. An array of structures is no different from an array of int. As long as your compare function can handle the structures correctly then there should be no problem.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    How would I access the individual parts of the struct from the compare function I tired -

    int strcomp = strcmp(arg1.firstname, arg2.firstname);

    and stuff but no luck

  4. #4
    Unregistered
    Guest
    here is an example of qsort with array of structures, copy and run on your compiler...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct  {
         char name[20];
         char number[5];
    	  } COMPANY;
    
    int compare1 (const void *a, const void *b);
    int compare2 (const void *a, const void *b);
    
    void main ()
    {
    	COMPANY customer[5]={
                         {"GREENS PAINTS", "2812"},
    	     {"ALBANY STORES", "5612"},
    	     {"STATIC DOORS", "4587"},
    	     {"DIBBLES GARDENS", "9587"},
    	     {"STATION GRADE", "5487"}
    	};
    	 COMPANY *str_ptr1, *str_ptr2;
    	 int i = 0;
    
    	 if((str_ptr1 =(COMPANY *) malloc (sizeof(COMPANY) * 5)) ==NULL) {
                 printf("\nUnable to allocate memory");
                 exit (1);
                  }
    	 printf("Before sort\n");
                     str_ptr2 = str_ptr1;
    	 while(i < 5) {
    		printf("\n%20s %5s", customer[i].name, customer[i].number);
    		strcpy(str_ptr2->name, customer[i].name);
    		strcpy(str_ptr2->number, customer[i].number);
    		str_ptr2++; i++;
                      }
    
                      str_ptr2 = str_ptr1;
    	  qsort(str_ptr2, 5, sizeof(COMPANY), compare1);
    	  printf("\n\nAfter first sort - by name\n");
    	  i = 0;
    	  str_ptr2 = str_ptr1;
    	  while(i < 5) {
    		strcpy(customer[i].name, str_ptr2->name);
    		strcpy(customer[i].number, str_ptr2->number);
    	                 printf("\n%20s %5s", customer[i].name, customer[i].number);
    		str_ptr2++;
    		i++;
    	 }
    	 str_ptr2 = str_ptr1;
    	 qsort(str_ptr2, 5, sizeof(COMPANY), compare2);
    	 printf("\n\nAfter second sort - by number\n");
    	 i = 0;
    	 str_ptr2 = str_ptr1;
    	 while(i < 5) {
    		strcpy(customer[i].name, str_ptr2->name);
    		strcpy(customer[i].number, str_ptr2->number);
    		printf("\n%20s %5s", customer[i].name, customer[i].number);
    		str_ptr2++;
    		i++;
    	 }
    	 str_ptr2 = str_ptr1;
    	 free(str_ptr1);
    	 printf("\nHit Return to quit");
    	 getchar();
    } /*main*/
    
    int compare1 (const void *a, const void *b)
    {
    	return ( strcmp(((COMPANY*)a)->name, ((COMPANY*)b)->name));
    }
    
    int compare2 (const void *a, const void *b)
    {
    	return ( strcmp(((COMPANY*)a)->number, ((COMPANY*)b)->number));
    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    here is an example of qsort with array of structures, copy and run on your compiler...
    SORRY GUYS FOR POSTING TWICE - ISP ERROR
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct  {
         char name[20];
         char number[5];
    	  } COMPANY;
    
    int compare1 (const void *a, const void *b);
    int compare2 (const void *a, const void *b);
    
    void main ()
    {
    	COMPANY customer[5]={
                         {"GREENS PAINTS", "2812"},
    	     {"ALBANY STORES", "5612"},
    	     {"STATIC DOORS", "4587"},
    	     {"DIBBLES GARDENS", "9587"},
    	     {"STATION GRADE", "5487"}
    	};
    	 COMPANY *str_ptr1, *str_ptr2;
    	 int i = 0;
    
    	 if((str_ptr1 =(COMPANY *) malloc (sizeof(COMPANY) * 5)) ==NULL) {
                 printf("\nUnable to allocate memory");
                 exit (1);
                  }
    	 printf("Before sort\n");
                     str_ptr2 = str_ptr1;
    	 while(i < 5) {
    		printf("\n%20s %5s", customer[i].name, customer[i].number);
    		strcpy(str_ptr2->name, customer[i].name);
    		strcpy(str_ptr2->number, customer[i].number);
    		str_ptr2++; i++;
                      }
    
                      str_ptr2 = str_ptr1;
    	  qsort(str_ptr2, 5, sizeof(COMPANY), compare1);
    	  printf("\n\nAfter first sort - by name\n");
    	  i = 0;
    	  str_ptr2 = str_ptr1;
    	  while(i < 5) {
    		strcpy(customer[i].name, str_ptr2->name);
    		strcpy(customer[i].number, str_ptr2->number);
    	                 printf("\n%20s %5s", customer[i].name, customer[i].number);
    		str_ptr2++;
    		i++;
    	 }
    	 str_ptr2 = str_ptr1;
    	 qsort(str_ptr2, 5, sizeof(COMPANY), compare2);
    	 printf("\n\nAfter second sort - by number\n");
    	 i = 0;
    	 str_ptr2 = str_ptr1;
    	 while(i < 5) {
    		strcpy(customer[i].name, str_ptr2->name);
    		strcpy(customer[i].number, str_ptr2->number);
    		printf("\n%20s %5s", customer[i].name, customer[i].number);
    		str_ptr2++;
    		i++;
    	 }
    	 str_ptr2 = str_ptr1;
    	 free(str_ptr1);
    	 printf("\nHit Return to quit");
    	 getchar();
    } /*main*/
    
    int compare1 (const void *a, const void *b)
    {
    	return ( strcmp(((COMPANY*)a)->name, ((COMPANY*)b)->name));
    }
    
    int compare2 (const void *a, const void *b)
    {
    	return ( strcmp(((COMPANY*)a)->number, ((COMPANY*)b)->number));
    }
    Last edited by bigtamscot; 03-07-2002 at 03:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21