delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/14/08:26:29

Message-ID: <346C51C6.A1012C33@cornell.edu>
Date: Fri, 14 Nov 1997 08:27:35 -0500
From: "A. Sinan Unur" <asu1 AT cornell DOT edu>
Reply-To: asu1 AT cornell DOT edu
Organization: Cornell University http://www.cornell.edu
MIME-Version: 1.0
To: Mike Collins <collim AT europe DOT stortek DOT com>, djgpp AT delorie DOT com
Subject: Re: Compiler warning - bad parameter
References: <64haqf$ks1 AT news DOT network DOT com>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019