qsort using function ptr

This is a discussion on qsort using function ptr within the C Programming forums, part of the General Programming Boards category; I have two structs one which holds car information. The function I am writing requires to sort the list by ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    24

    qsort using function ptr

    I have two structs one which holds car information. The function I am writing requires to sort the list by an entered key. The list struct holds the function ptr to use. Can I use this pointer in qsort? I have tried
    qsort(pcl->list,pcl->size,sizeof(pcl->list[i]),pcl->CompareFPtr);
    but it gives the warning: parsing of arg 4 'qsort' from incompatible pointer type.

    any ideas??

    typedef struct
    {
    char *dynamicMake;
    char *dynamicModel;
    int year;
    char rego[7];
    } Car;

    typedef struct List
    {
    Car list[LISTSIZE];
    unsigned size;
    int(*CompareFPtr)(Car*,Car*);
    } List;

    Any light shed appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Look at this:
    Code:
    #include <stdlib.h>
    void qsort( void *base,
                size_t num,
                size_t width,
                int (*compar) ( const void *, 
                                const void *) );
    The prototype differs to what you are using as the 4th arg:
    >>int(*CompareFPtr)(Car*,Car*);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    I have changed the function pointer to

    [code]/*

    int(*CompareFPtr)(const void*,const void*);
    /*[code]

    and still give parse error message is the correct syntax for a struct (pcl) holding a function pointer for the 4th arg of qsort

    pcl->CompareFPtr ??

    can i use a function pointer in arg 4 of qsort??

    thanks

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    Thanks, fixed it
    I had a structure with the function pointer stored ie

    [code]*/
    typedef struct List
    {
    Car list[LISTSIZE];
    unsigned size;
    int(*CompareFPtr)(const void*,const void*);
    } List;

    [code]

    I have 4 different compare functions I store the relevant one to user in the struct function pointer. ie

    [code]
    int(*MakeCompare) (Car*, Car*),
    int(*ModelCompare) (Car*, Car*),int(*YearCompare) (Car*, Car*),
    int(*RegCompare) (Car*, Car*));
    [code]/*

    I have change my function pointer parameters to void and then type cast the relevant compare function when assigning it ie

    [code]
    pcl->CompareFPtr=(int (*)(const void*, const void*))MakeCompare;
    [code]

    i can now use qsort...
    works fine and minimum changes.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    nomes, have a read of this thread:
    http://www.cprogramming.com/cboard/s...threadid=13473

    It'll teach you about code tags. Alternatively, just look at my signature.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    24
    Code:
    /*
    
    printf("Sorry about that!! Have it sussed now!");
    
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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