fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <search.h>
  4.  
  5. static char *data[] = { "alpha", "bravo", "charlie", "delta",
  6. "echo", "foxtrot", "golf", "hotel", "india", "juliet",
  7. "kilo", "lima", "mike", "november", "oscar", "papa",
  8. "quebec", "romeo", "sierra", "tango", "uniform",
  9. "victor", "whisky", "x-ray", "yankee", "zulu"
  10. };
  11.  
  12. int
  13. main(void)
  14. {
  15. ENTRY e, *ep;
  16. int i;
  17.  
  18. hcreate(30);
  19.  
  20. for (i = 0; i < 24; i++) {
  21. e.key = data[i];
  22. /* data is just an integer, instead of a
  23.   pointer to something */
  24. e.data = (void *) i;
  25. ep = hsearch(e, ENTER);
  26. /* there should be no failures */
  27. if (ep == NULL) {
  28. fprintf(stderr, "entry failed\n");
  29. exit(EXIT_FAILURE);
  30. }
  31. }
  32.  
  33. for (i = 22; i < 26; i++) {
  34. /* print two entries from the table, and
  35.   show that two are not in the table */
  36. e.key = data[i];
  37. ep = hsearch(e, FIND);
  38. printf("%9.9s -> %9.9s:%d\n", e.key,
  39. ep ? ep->key : "NULL", ep ? (int)(ep->data) : 0);
  40. }
  41. hdestroy();
  42. exit(EXIT_SUCCESS);
  43. }
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
   whisky ->    whisky:22
    x-ray ->     x-ray:23
   yankee ->      NULL:0
     zulu ->      NULL:0