home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!caen!zaphod.mps.ohio-state.edu!not-for-mail
- From: ren@function.mps.ohio-state.edu (Liming Ren)
- Newsgroups: comp.lang.c
- Subject: Question about sample program in the man page of hsearch (hash functions)!
- Date: 10 Nov 1992 12:25:26 -0500
- Organization: Department of Mathematics, The Ohio State University
- Lines: 101
- Distribution: world
- Message-ID: <1dora6INNdgv@function.mps.ohio-state.edu>
- NNTP-Posting-Host: function.mps.ohio-state.edu
-
- I want to learn how to use hsearch functions. I got a sample program from the man page of hsearch on UNIX.
- saddly, the program did not work . What's weired was after I made an insignifficant change, it worked.
- I can see there are some diffenence. But I don't see what caused the problem in the old one (ss.c).
- It is also hard to believe the program in the man page has problems. Before this is clear to me, I don't feel
- safe to use it. PLEASE HELP!
-
- The difference is shown as follows: (where ss.c is the original program, ttt.c is the modified one)
-
-
- [11:56am][/n/function/1/ren][29] diff ss.c ttt.c
- 18,19c18,19
- < while ((scanf("%s%d%d", str_ptr, &info_ptr->age, <-------In ss.c
- < &info_ptr->room) !=EOF)&& (i++ <NUM_EMPL)) {
- ---
- > while ((i++ <NUM_EMPL) &&scanf("%s%d%d", str_ptr, &info_ptr->age, <-------In ttt.c
- > &info_ptr->room) !=EOF){
-
-
- The following is a test run of this two programs. I used cc -g to compile them.
- [11:53am][/n/function/1/ren][31] ss
- qqq 1 2
- www 4 5
- 555 6 7
- 123 5 6
- enter done!
- qqq
- no such employee qqq <------------------------------not right!!!
- www
- no such employee www
- 555
- found 555, age = 6, room = 7
- 123
- no such employee 123
- ^D[11:54am][/n/function/1/ren][32] ttt <---------all right.
- qqq 1 2
- www 4 5
- 555 6 7
- enter done!
- qqq
- found qqq, age = 1, room = 2
- www
- found www, age = 4, room = 5
- 555
- found 555, age = 6, room = 7
-
-
- End of session!!!!
-
-
- Here is the program ss.c obtained by "man hsearch"
-
-
- #include <stdio.h>
- #include <search.h>
- struct info { /* this is the info stored in the table */
- int age, room; /* other than the key. */
- };
- #define NUM_EMPL 3 /* # of elements in search table */
- main( )
- {
- char string_space[NUM_EMPL*20];
- struct info info_space[NUM_EMPL];
- char *str_ptr = string_space;
- struct info *info_ptr = info_space;
- ENTRY item, *found_item, *hsearch( );
- char name_to_find[30];
- int i = 0;
- /* create table */
- (void) hcreate(NUM_EMPL);
- while ((scanf("%s%d%d", str_ptr, &info_ptr->age,
- &info_ptr->room) !=EOF)&& (i++ <NUM_EMPL)) {
- /* put info in structure, and structure in item */
- item.key = str_ptr;
- item.data = (char *)info_ptr;
- str_ptr += strlen(str_ptr) + 1;
- info_ptr++;
- /* put item into table */
- (void) hsearch(item,ENTER);
- }
- /* access table */
- printf("enter done!\n");
- item.key = name_to_find;
- while (scanf("%s", item.key) != EOF) {
- if ((found_item = hsearch(item,FIND)) != NULL) {
- /* if item is in the table */
- (void)printf("found %s, age = %d, room = %d\n",
- found_item->key,
- ((struct info *)found_item->data)->age,
- ((struct info *)found_item->data)->room);
- } else {
- (void)printf("no such employee %s\n", name_to_find);
- }
- }
- }
-
-
-
-
-
-
-
-