home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: Qusetions about UC Berkerley header file. Help!
- Message-ID: <9236321.28830@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <1hlrgtINNs0e@function.mps.ohio-state.edu>
- Date: Mon, 28 Dec 1992 10:27:18 GMT
- Lines: 59
-
- ren@function.mps.ohio-state.edu (Liming Ren) writes:
-
- >This question bugs me all the day. I hope it makes sense.
- >
- >I am reading two header files distributed by UC Berkley. One is search.h
- >and the other one is db.h (they are about hashing, Btree etc).
- >
- >(1) In search.h, there are several lines such as:
- >
- >int hcreate __P((unsigned int));
- >void hdestroy __P((void));
- >ENTRY *hsearch __P((ENTRY, ACTION));
- >
- >I don't understand this. It seems __P is a function returns ....(confused
- >here). What are hcreate , hdestroy etc anyway?
-
- __P will actually be a *macro*, defined by something like this:
-
- #ifdef __STDC__
- #define __P(decl) (decl) /* Use ANSI prototypes */
- #else
- #define __P(decl) () /* Use old-style declarations */
- #endif
-
- The idea here is to be able give one declaration for each function,
- using the __P macro so that it works for both ANSI C and K&R C.
- For ANSI C, the declaration for hcreate() will expand to
- int hcreate (unsigned int);
- and for K&R C it will expand to
- int hcreate ();
-
- >(2) In db.h, there is a definition for DB:
- >
- >typedef struct __db {
- > DBTYPE type; /* underlying db type */
- > int (*close) __P((struct __db *));
- > int (*del) __P((const struct __db *, const DBT *, unsigned int));
- > int (*get) __P((const struct __db *, const DBT *, DBT *,
- > unsigned int));
- > int (*put) __P((const struct __db *, const DBT *, const DBT *,
- > unsigned int));
- > int (*seq) __P((const struct __db *, DBT *, DBT *, unsigned int));
- > int (*sync) __P((const struct __db *));
- > void *internal; /* access method private */
- >} DB;
-
- This declares a structure which contains various pointers to functions.
- For example, given
- DB x;
- then x.close is a pointer to a function taking a pointer to struct __db and
- returning an int.
- (Pointers in structures like this are generally used in C to do the same sort
- of things that can be achieved with virtual functions in C++.)
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature virus is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-