#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2

    qsort() function: How to work correctly with it ?


    I wrote an array and a char_compare function for qsort:
    Code:
        char arr[]={'1', '2', '0'};
    
    
        
    char char_compare(char a, char b)
    {
        return a>b ? a: b;
    } 
     
        qsort(arr,3,sizeof(char),char_compare);
    
    
        
        int i;
    
    
          for(i=0;i<3;++i)
            printf("%c\t",arr[i]); /*Unsorted output is: "1   2   0"   */
    However, the compiler gives me the following message:
    "PASSING ARG 4 OF 'QSORT' FROM INCOMPATIBLE POINTER TYPE". >>>How do i fix it ??

    >>>Also, if my array consists of strings of different length, what size-of-object should i PASS TO qsort ??(arg 2)

    like this:
    Code:
    char *arry={"hello", "tt", "pointers"};
    What is the size of element here ??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Have you looked for some documentation for qsort()? Look closely at the parameter types for the comparison function.

    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    2
    Originally Posted by jimblumberg
    Have you looked for some documentation for qsort()? Look closely at the parameter types for the comparison function.

    Jim
    Thanks, i kinda managed to work it out.

IMN logo majestic logo threadwatch logo seochat tools logo