www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/04/02:24:21

Message-ID: <32F6E31F.34EA@eik.bme.hu>
Date: Tue, 04 Feb 1997 08:19:59 +0100
From: "DR. Andras Solyom" <solyom AT eik DOT bme DOT hu>
Reply-To: solyom AT eik DOT bme DOT hu
Organization: Technical University of Budapest
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Is this a bug?

I wanted to use 'bsearch' on an ordered set of structures. using a
member of the structure as a key. I tried:

typedef struct {
	long lElement;
	....
}ANYSTRUCT;

ANYSTRUCT anyStruct, *pArray, *anyElem;

int main()
{
  pArray = malloc( size * sizeof(ANYSTRUCT) );
....
 anyElem =  
 bsearch( (void *)anySTruct->lElement, pArray, size, sizeof(ANYSTRUCT),
               bSearchFunc);
It did not work. The pointer to the key value that bSearchFunc received
was some rubbish. Then I tried 

 bsearch( &(anySTruct->lElement), pArray, size, sizeof(ANYSTRUCT),
               bSearchFunc_1);

which also did not work correctly (bSearchFunc_1 differs from
bSearchFunc, because it receives a pointer and not a value casted to a
pointer). The working solution was:

 l = anySTruct->lElement;
 bsearch( (void *)l, pArray, size, sizeof(ANYSTRUCT),
               bSearchFunc);

Is this a correct behaviour of bsearch? Or is this a compiler bug? I
compiled without optimization with -Wall and -g. Gdb has shown that the
argument of bsearch was incorrect in the first two cases.

					Andras

- Raw text -


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