tsearch(3) return value?

jgp at moscom.UUCP jgp at moscom.UUCP
Mon Feb 23 15:32:56 AEST 1987


I'm having some trouble with tsearch.  According to SYSVID (#2, v1 p239):
	"If the datum is found, tsearch returns a pointer to it.
	If not, tsearch returns a pointer to the inserted item."

To me, that sounds like tsearch should be returning a pointer to my
structure "struct mine *tsearch()".  After spending some time trying
to figure out why my program wasn't working, I determined that on my
system, tsearch was returning a pointer to a pointer to my structure
"struct mine **tsearch()".

Does anyone know which way it is supposed to work?  On both the systems
I have access to (NCR Tower 32/600 and AT&T 3b2/310 both with System V.2)
I get the double indirection which would lead me to suspect that SYSVID
is wrong (or that I'm just misinterpreting it).

The following program demonstrates the problem.  The output I get is:
	27a0 hello
	** 27a0 hello
	*  3c68 
According to SYSVID I would assume the '*' to be correct but the '**'
is what is being generated.

If anybody can either reconcile the above behavior with SYSVID or
mention systems that use the single indirection, I would be grateful.

#include	<stdio.h>
#include	<search.h>

int sqncmp();
char *tsearch();

struct foos {
	char *s;
	short c;
} foo = { "hello", 4 };
struct foos *root1 = NULL;
struct foos *root2 = NULL;

main()
{
	struct foos *p, **pp;
	
	printf("%lx %s\n", (long)&foo.s, foo.s);
	pp = (struct foos **)tsearch((char *)&foo, (char **)&root1, sqncmp);
	p = (struct foos *)tsearch((char *)&foo, (char **)&root2, sqncmp);
	printf("** %lx %s\n", (long)&(*pp)->s, (*pp)->s);
	printf("*  %lx %s\n", (long)&p->s, p->s);
}

int
sqncmp(p, q)
struct foos *p, *q;
{
	return strcmp(p->s, q->s);
}
-- 
Jim Prescott	rochester!moscom!jgp



More information about the Comp.lang.c mailing list