1835196 Members
2157 Online
110077 Solutions
New Discussion

Re: Problem with qsort

 
SOLVED
Go to solution
Travis Rebok
Advisor

Problem with qsort

I'm upgrading my application from HP-UX 10.20 to HP-UX 11.0. Recompiling program I am getting these compiler warnings:
cc: warning 604: Pointers are not assignment-compatible. cc: warning 563: Argument #4 is not the correct type.

This is the line of code:
qsort(usocs, 18, sizeof(usocs[0]), strcmp);

I have included , so I am using the HP-UX strcmp function.

To fix I have even tried declaring a function pointer and assigning the strcmp function to it:
int (*fn_pointer) (const void *, const void *);
fn_pointer = strcmp;
qsort(usocs, 18, sizeof(usocs[0]), fn_pointer);

When I compile this fix I get the following compiler warning for the line "fn_pointer = strcmp;":
cc: warning 604: Pointers are not assignment-compatib
le.

According to what I have read, this fix should work.

Any suggestions?
6 REPLIES 6
Elmar P. Kolkman
Honored Contributor

Re: Problem with qsort

Do you get a warning from the qsort line as well in the solution you gave yourself? If not, you could get rid of the warning by checking out the include file where your prototype for strcmp is declared. Apperently it is not included. Or it is complaining about the arguments of strcmp, which are not of the void type.

You could try to add the prototype in your code:
int strcmp(const void*,const void*);

That should fix it, or tell you where the real prototype is defined.

Or you could just ignore it, as it is just a warning.
Every problem has at least one solution. Only some solutions are harder to find.
Travis Rebok
Advisor

Re: Problem with qsort

When I made the initial fix, the warning message "cc: warning 604: Pointers are not assignment-compatible." was given for the line: fn_pointer = strcmp;

I no longer got a warning message on the qsort line itself.

The strcmp function is used other places in the program and those lines are not being flagged, so I know that the compile/link can find strcmp in

I added the following line just to be sure:
int strcmp(const void*,const void*);

So it appears that the problem is just with the statement: fn_pointer = strcmp


A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Problem with qsort

Hang on for some world-class ugly type casting:

qsort(usocs, 18, sizeof(usocs[0]),strcmp);

should be:

qsort((void *) usocs,(size_t) 18,sizeof(usocs[0]),
(int (*)(const void *a, const void *b))strcmp);

If I've done counted my paren's right, that there ought to do it.
If it ain't broke, I can fix that.
Travis Rebok
Advisor

Re: Problem with qsort

That's ugly, but it worked.

A couple of questions though:

I'm not familiar with size_t.
What is it and why do I need it?

Do you know why I was getting the
"cc: warning 604: Pointers are not assignment-compatible" warning when trying to assign: "fn_pointer = strcmp;" in my previous fix?
A. Clay Stephenson
Acclaimed Contributor

Re: Problem with qsort

If you do a man qsort you will see that the size_t type is actually the correct type for the nel and size parameters. The prototype types for ANSI C actually take care of this for you but us anal retentive types don't trust those integer (the implicit type of an integer constant)to size_t conversions. Size_t is used because ints on some implementations were 16-bits, 32-bits, 64-bits, or 128 bits. This is a way to standardize the stdlib functions. The pseudo function sizeof() returns a size_t value for just this reason --- and you will not I did not type cast it in the qsort call.


The type cast was also the problem with your fn_pointer assignment so the same fix will fix you there and would not then require the type cast in the qsort actual parameters.

int (*fn_pointer)(const void *a, const void *b) = NULL;

fn_pointer = (int (*)(const void *a, const void *b)) strcmp;
qsort((void *) usocs,(size_t) 18, sizeof(usocs[0]),fn_pointer);

I hope I've counted them paren's right again cause it gets to be a mite tricky.

If it ain't broke, I can fix that.
Travis Rebok
Advisor

Re: Problem with qsort

Clay -
Great explanation. Thanks for the help!