Mail Archives: djgpp/1997/11/14/08:26:29
Mike Collins wrote:
> I am writing a program which involves qsort(). When I compile it, I 
> get a warning message :
> 
>  Compiling: test2.c
>  In function `main':
>  test2.c(19) Warning:  passing arg 4 of `qsort' from incompatible 
> pointer type  no errors
> 
> The program runs fine, though not in the "bare-bones" form below, of 
> course. This is simply the minimum program that still gives the 
> compile warning. I want to get rid of the warning, but though I have 
> tried a lot of different things, I can't see where the incompatibility 
> in the pointers lies. Another compiler does not give any warning.
> 
> 
> The program is below:
> 
> #include <stdio.h>
> 
> struct BOOKER_ITEM
> { char refnum[7];
> };
> 
> // func definition pulled out of <stdlib.h>
it is a decleration, not definition.
> 
> void    qsort(void *_base, size_t _nelem, size_t _size,
>               int (*_cmp)(const void *_e1, const void *_e2));
you can see here that the comparison function is supposed to take two
const void pointers and return an int.
> int comp_ref(struct BOOKER_ITEM *s1, struct BOOKER_ITEM *s2);
yours takes two non-const struct BOOKER_ITEM pointers. write your
function like this:
int comp_ref(const void *p1, const void *p2)
{
 const struct BOOKER_ITEM *s1 = p1;
 const struct BOOKER_ITEM *s2 = p2;
... rest of the function.
}
> 
> main()
> {
>   int known_records;
>   struct BOOKER_ITEM *book_lst;
> 
>   qsort(book_lst, known_records, sizeof(struct BOOKER_ITEM), comp_ref);
> }
> 
> int comp_ref(struct BOOKER_ITEM *s1, struct BOOKER_ITEM *s2)
> { return strncmp(s1->refnum, s2->refnum, sizeof(s1->refnum));
> }
i am not sure on this but i hope you are not assuming sizeof will return
the length of the string stored in s1->refnum ... it will always give 7.
this and other C related questions are better addressed to comp.lang.c
or comp.lang.c.moderated rather than comp.os.msdos.djgpp.
-- 
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA
mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/
- Raw text -