-
July 17th, 2009, 01:58 PM
#1
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>
-
July 17th, 2009, 02:05 PM
#2
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.
-
July 17th, 2009, 02:12 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|