home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18926 < prev    next >
Encoding:
Text File  |  1992-12-28  |  2.7 KB  |  71 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Qusetions about UC Berkerley header file. Help!
  5. Message-ID: <9236321.28830@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1hlrgtINNs0e@function.mps.ohio-state.edu>
  9. Date: Mon, 28 Dec 1992 10:27:18 GMT
  10. Lines: 59
  11.  
  12. ren@function.mps.ohio-state.edu (Liming Ren) writes:
  13.  
  14. >This question bugs me all the day. I hope it makes sense.
  15. >
  16. >I am reading two header files distributed by UC Berkley. One is search.h
  17. >and the other one is db.h (they are about hashing, Btree etc).
  18. >
  19. >(1) In search.h, there are several lines such as:
  20. >
  21. >int      hcreate __P((unsigned int));
  22. >void     hdestroy __P((void));
  23. >ENTRY   *hsearch __P((ENTRY, ACTION));
  24. >
  25. >I don't understand this. It seems __P is a function returns ....(confused 
  26. >here).  What are hcreate , hdestroy etc anyway?
  27.  
  28. __P will actually be a *macro*, defined by something like this:
  29.  
  30.     #ifdef __STDC__
  31.     #define __P(decl) (decl)    /* Use ANSI prototypes */
  32.     #else
  33.     #define __P(decl) ()        /* Use old-style declarations */
  34.     #endif
  35.  
  36. The idea here is to be able give one declaration for each function, 
  37. using the __P macro so that it works for both ANSI C and K&R C.
  38. For ANSI C, the declaration for hcreate() will expand to
  39.     int hcreate (unsigned int);
  40. and for K&R C it will expand to
  41.     int hcreate ();
  42.  
  43. >(2) In db.h, there is a definition for  DB:
  44. >
  45. >typedef struct __db {
  46. >        DBTYPE type;                    /* underlying db type */
  47. >        int (*close)    __P((struct __db *));
  48. >        int (*del)      __P((const struct __db *, const DBT *, unsigned int));
  49. >        int (*get)      __P((const struct __db *, const DBT *, DBT *,
  50. >                            unsigned int));
  51. >        int (*put)      __P((const struct __db *, const DBT *, const DBT *,
  52. >                            unsigned int));
  53. >        int (*seq)      __P((const struct __db *, DBT *, DBT *, unsigned int));
  54. >        int (*sync)     __P((const struct __db *));
  55. >        void *internal;                 /* access method private */
  56. >} DB;
  57.  
  58. This declares a structure which contains various pointers to functions.
  59. For example, given
  60.     DB x;
  61. then x.close is a pointer to a function taking a pointer to struct __db and
  62. returning an int.
  63. (Pointers in structures like this are generally used in C to do the same sort
  64. of things that can be achieved with virtual functions in C++.)
  65.  
  66. -- 
  67. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  68. This .signature virus is a self-referential statement that is true - but 
  69. you will only be able to consistently believe it if you copy it to your own
  70. .signature file!
  71.