CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2009
    Posts
    2

    Strange qsort issue in C/GCC

    I have written the code below attempting to narrow down my problem to a small section of code to get some help with the issue. It returns the warning:
    "tmain.c:16: warning: passing argument 4 of ‘qsort’ from incompatible pointer type"

    The code seems to work in that it will actually sort the array, but I cant help but wonder if something is slightly wrong and will blow up later.
    I am running Fedora 10 and compiling it with gcc -c [file name] (gcc version 4.3.2 20081105 (Red Hat 4.3.2-7) (GCC) )
    Thank you for the help.

    <code>
    #include<stdio.h>
    #include<stdlib.h>
    int compareFloat(void *vp, void *vq)
    {
    float *p = vp;
    float *q = vq;
    float x;
    x = *p - *q;
    return (int)((x==0.0) ? 0 : (x < 0.0) ? -1 : +1);
    }
    int main(void)
    {
    float meh[4] = {4,3,2,1};
    qsort(meh,4,sizeof(float),compareFloat);
    printf("%e\n",meh[0]);
    }
    </code>

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Strange qsort issue in C/GCC

    The reason is that the comparison function is supposed to have const void* parameters, but you defined your comparison function with non-const void* parameters.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2009
    Posts
    2

    Re: Strange qsort issue in C/GCC

    Thank you. Managed to completely miss that in the docs somehow.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured