3 Replies - 1920 Views - Last Post: 15 November 2011 - 03:45 AM Rate Topic: -----

#1 Xing   User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

C's qsort ( )

Posted 06 October 2006 - 06:44 PM

Description: qsort() sorts a table of data.
/* Headers */
#include <stdio.h>
#include <stdlib.h>

/* Comparison Function */
int compare (const void * a, const void * b){
  return ( *(char*)a - *(char*)b );
}

int main (){

  int i;
  /* Elements to be sorted */
  char array[] = { 'h','f','a','j','e','g','c','i','d','b' };
  
  /* Sorting the elements */
  qsort (array, 10, sizeof(char), compare);
  
  /* Displaying the elements */
  for (i=0; i<10; ++i){
    printf ("%c ",array[i]);
  }
  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C's qsort ( )

#2 Tennison   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-February 08

Re: C's qsort ( )

Posted 09 April 2008 - 04:01 AM

"return ( *(char*)a - *(char*)b );"- could u explain this recursive call??
Was This Post Helpful? 0
  • +
  • -

#3 adtd8   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-July 10

Re: C's qsort ( )

Posted 06 July 2010 - 04:14 PM

Great code snippet. Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 peace_fixation   User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Re: C's qsort ( )

Posted 15 November 2011 - 03:45 AM

"return ( *(char*)a - *(char*)b );"- could u explain this recursive call?? *a means the value stored at the location pointed to by 'a' (you dereference the pointer) *(char*)a is the same, but you cast the void* pointer 'a' to a char* pointer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1