Thread: problem with incompatible pointer type

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

    problem with incompatible pointer type

    When compiling one of my function, it gives me a warning message
    "passing arg 4 of 'qsort' from incompatible pointer type"
    what does this mean?

    here's some of the codes that's related
    Code:
    int compare(int *key1, int *key2)
    {
     return (*key1 - *key2);
    }
    
    void mysort(int *keyArray, int nel, int width, int (*compare)(int *, int *))
    {
     qsort(keyArray, nel, width, compare);
    }
    and I have initialised
    int *keyArray;
    in my main function

    did I pass in some wrong arguments into the library function qsort?


    Code tags added by kermi3

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdlib.h>
    void qsort( void *base,
                size_t num,
                size_t width,
                int (*compar) ( const void *, 
                                const void *) );
    Note that the function ptr in the 4th parm takes void pointers, you are using int pointers.

    From the manual, here's an example of a char* compare function. I'll leave it to you to change it to int*:
    Code:
    int compare( const void *op1, const void *op2 )
      {
        const char **p1 = (const char **) op1;
        const char **p2 = (const char **) op2;
        return( strcmp( *p1, *p2 ) );
      }
    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
    16
    Code:
    int compare( const void *op1, const void *op2 )
      {
        const char **p1 = (const char **) op1;
        const char **p2 = (const char **) op2;
        return( strcmp( *p1, *p2 ) );
      }
    When I tried that, I get warnings for
    const char **p1 = (const char **)op1; and op2

    "declaration of 'op1' shadows a parameter"


    Code tags added by kermi3

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An int version (untested)
    Code:
    int compare(void *p1, void *p2)
    {
       int *key1 = (int *) p1;
       int *key2 = (int *) p2;
       return (*key1 - *key2);
    }
    Go from there...

    (my prev post was only a sample for you to review, not to actually use directly in your code).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    thanks
    now when I want to use the function in my main function
    I get parse error

    I tried to do mysort(keyArray, 50, 30, compare(const void *,const void *);

    and mysort is
    Code:
    void mysort(int *keys, int nel, int width, int (*compare)(const void *, const void *) )
    {
    qsort(keys, nel, width, compare);
    return;
    }
    Code tags added by kermi3

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by SIKCAR
    thanks
    now when I want to use the function in my main function
    I get parse error

    I tried to do mysort(keyArray, 50, 30, compare(const void *,const void *);

    and mysort is
    void mysort(int *keys, int nel, int width, int (*compare)(const void *, const void *) )
    {
    qsort(keys, nel, width, compare);
    return;
    }
    count your parentheses
    hello, internet!

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    sorry, I missed a ) when I typed it here, but that wasn't the problem

    basically I get
    parse error before 'const'

    in an example I've got, they were comparing char
    but they could do
    Code:
    mysort(charArray, 50, sizeof(char), compare);
    I've been trying to do that, but get the error I've got in the first place

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, here's some complete code. There's a different in the compare function to the one I posted earlier, both of it's parameters are declared as const void* now.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printarray(int a[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
            printf ("%d\n", a[i]);
    }
    
    int compare(const void *p1, const void *p2)
    {
       int *key1 = (int *) p1;
       int *key2 = (int *) p2;
       return (*key1 - *key2);
    }
        
    int main(void)
    {
        int myarray[] = {5, 4, 1, 9, 5, 10};
        int num_elements = sizeof(myarray)/sizeof(myarray[0]);
    
        puts("Before");
        printarray(myarray, num_elements);
        qsort (myarray, num_elements, sizeof(int), compare);
        puts("After");
        printarray(myarray, num_elements);
        
        return 0;
    }
    
    /* Program output
    Before
    5
    4
    1
    9
    5
    10
    After
    1
    4
    5
    5
    9
    10
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM