Article View: gnu.gcc.help
Article #49496Re: bsearch argument
From: Kevin Rodgers
Date: Tue, 18 Jan 1994 18:01
Date: Tue, 18 Jan 1994 18:01
64 lines
2614 bytes
2614 bytes
[Mail to sternber@haas.berkeley.edu and sternber@sba60.berkeley.edu (the
address suggested by netfind) both failed; please get your mailers
fixed.]
In article <2heefv$btl@agate.berkeley.edu> sternber@haas.berkeley.edu (Theodore Sternberg) writes:
>What does...
>test.c:11: warning: passing arg 5 of `bsearch' from incompatible pointer type
>...mean? Here's the code (which, incidentally, raises no problems
>with ULTRIX's cc)...
>
>#include <stdlib.h>
>#include <string.h>
>
>int charcompare( char, char );
>
>main() {
> char str1[10], *str2;
> strcpy( str1, "abcdefghi" );
>
> str2 = bsearch( (char *)'e', str1, 9, 1, charcompare );
>}
>/*---------------------------------*/
>
>int charcompare( char ch1, char ch2 ) {
> return ch1 - ch2;
>}
Well, the 5th argument in your call to bsearch is charcompare, which is
a [pointer to a] function of two arguments (a char and a char) that
returns an int. The SunOS 4.1.3 man page for bsearch says only that
"the comparison function ... is called with two arguments that point to
the elements being compared." The "POSIX Programmer's Guide" by Donald
Lewine (O'Reilly & Associates) gives this declaration:
int (*compar) (const void *, const void *);
Your charcompar function is incompatible with the declared type of
bsearch's 5th argument, because its argument types are not pointers; you
need to change
int charcompare( char, char );
to
int charcompare(const char *, const char *);
and
int charcompare (char ch1, char ch2)
{
return ch1 - ch2;
}
to
int charcompare (const char *ch1, const char *ch2)
{
return *ch1 - *ch2;
}
--
Kevin Rodgers kevin@traffic.den.mmc.com
Martin Marietta MS DC5695 (303) 971-2396 [voice] / -1666 [fax]
P.O. Box 179 "Shoulder to shoulder, we will
Denver CO 80201 USA Fight! Fight! Fight, Fight, Fight!"
Technical Details
Message-ID:
<1994Jan18.180118.22128@den.mmc.com>Path:
arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!vixen.cso.uiuc.edu!sdd.hp.com!col.hp.com!csn!news.den.mmc.com!kevinReferences:
<2heefv$btl@agate.berkeley.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,57e68b420a2c84dc
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1994-01-18 16:53:53 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!vixen.cso.uiuc.edu!sdd.hp.com!col.hp.com!csn!news.den.mmc.com!kevin
From: kevin@traffic.den.mmc.com (Kevin Rodgers)
Subject: Re: bsearch argument
Message-ID: <1994Jan18.180118.22128@den.mmc.com>
Sender: news@den.mmc.com (News)
Nntp-Posting-Host: traffic.den.mmc.com
Organization: Martin Marietta Internal Information Systems (Western Region),
Technology Support
References: <2heefv$btl@agate.berkeley.edu>
Date: Tue, 18 Jan 1994 18:01:18 GMT
Lines: 64