Jack Klein wrote:
> On Mon, 24 Nov 2008 19:54:05 -0800 (PST), wrote in
> comp.lang.c:
>
>> i'm trying to port some old code to a more current linux compiler.
>> i've run into an error that i
>> have been at a loss to correct. the following, very brief, sections
>> compile and generate executables
>> under pcdos turbo-c and gcc 3.3.6 (slackware 10.1). i've not included
>> much code, so i hope
>> someone has come across this before and can recognize it.
>>
>> this is defined in .h:
>> typedef struct {
>> unsigned attr;
>> unsigned valu;
>> char oname[7];
>> } OPCODE;
>>
>> code section with error:
>>
>> 110 OPCODE *find_code(nam)
>> 111 char *nam;
>> 112 {
>> 113 OPCODE *bsearch();
>> 114
>> 115 static OPCODE opctbl[] = {
>> 116 { AJMP, 0x11,
>> "ACALL" },
>> 117 { ADD + ((R7 - A) << 5) + A, 0x24,
>> "ADD" },
>>
>>
>> under gcc 4.2.1 (suse 10.3) i get the following error:
>>
>> util.c:113: error: conflicting types for ‘bsearch’
>> /usr/include/stdlib.h:775: error: previous declaration of ‘bsearch’
>> was here
>>
>> i did not write this code and i'm still trying to understand the
>> authors use of
>> bsearch return as a pointer. problem is, i know it "used" to work...
>>
>> any suggestions?
>> thanks.
>
> Your compiler is telling you what the problem is. The standard header
> <stdlib.h> declares a prototype for the standard C library function
> bsearch(), including its return type, which is not a pointer to an
> OPCODE struct.
>
> When you compile this code, you are directly or indirectly including
> <stdlib.h>, so the compiler has the correct prototype in scope. When
> it comes across the prototype in your source file, with a different
What's on line 113 is a declaration for bsearch(), not a prototype.
> return type, it quite rightly complains.
>
> The minimal proper solution is to remove the incorrect prototype from
> the source file, since is already has the proper prototype.
While that is the minimum change that might make the program correct,
there are other things that need to be done as well as, or instead of,
removing line 113.
Before doing anything else, determine whether or not the program
provides it's own definition of a function named bsearch(). If it does,
rename that function, both in the definition, and also in every call to
the function, so it doesn't conflict with the standard library function.
If there is no such definition in the code, remove line 113, and then
check to make sure that the code uses bsearch() in a way consistent with
the requirements of the C standard library. To a considerable extent,
any decent compiler with warning levels set high will catch many of
possible problems, just because of the prototype declared in <stdlib.h>.
However, it's possible that there's some more subtle issues, so check
each call to bsearch() and make sure you understand what it's doing, and
that it's doing it correctly.