Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!seismo!rochester!ritcv!moscom!jgp From: jgp@moscom.UUCP Newsgroups: comp.lang.c,comp.unix.questions Subject: tsearch(3) return value? Message-ID: <915@moscom.UUCP> Date: Mon, 23-Feb-87 00:32:56 EST Article-I.D.: moscom.915 Posted: Mon Feb 23 00:32:56 1987 Date-Received: Tue, 24-Feb-87 02:51:39 EST Reply-To: jgp@moscom.UUCP (Jim Prescott) Distribution: world Organization: MOSCOM Corp, E Rochester, NY, USA Lines: 57 Xref: utgpu comp.lang.c:1135 comp.unix.questions:1150 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 #include 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