home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!asuvax!chnews!sedona!bhoughto
- From: bhoughto@sedona.intel.com (Blair P. Houghton)
- Newsgroups: comp.lang.c
- Subject: Re: Help!!!!!!!!!!!!
- Date: 14 Dec 1992 21:40:49 GMT
- Organization: Intel Corp., Chandler, Arizona
- Lines: 68
- Message-ID: <1giv11INNptr@chnews.intel.com>
- References: <1992Dec12.184235.29056@sbcs.sunysb.edu> <1992Dec14.174931.4650@rexago8.uucp>
- NNTP-Posting-Host: alfalfa.intel.com
-
- In article <1992Dec14.174931.4650@rexago8.uucp> aa@rexago8.uucp (Adam Andrews) writes:
- >jcontro@engws10.ic.sunysb.edu () writes:
- >> SOME_STUCT *Ptr;
- >>fn( ..., Ptr );
- >
- >Try passing it like: fn( ..., &Ptr);
- >and receiving it like: fn ( SOME_STRUCT **Ptr) { ...
-
- Try having fn() create the list from whole cloth
- and return the list:
-
- typedef struct ... SOME_STUCT;
-
- ...
- {
- SOME_STUCT *Ptr;
- ...
- Ptr = fn(); /* gimme da list */
- ...
- }
-
- /* just a little OOP */
- SOME_STUCT *new_stuct( void )
- {
- SOME_STUCT *new;
-
- new = (SOME_STUCT *) malloc ( sizeof(SOME_STUCT) );
- if ( !new ) {
- ... /* barf and die */
- }
-
- new->foo = NULL; /* zero members appropriately */
- new->bar = 0;
- new->bazz = 0UL;
- ...
- new->next = new->prev = NULL;
-
- return new;
- }
-
- SOME_STUCT *fn( void /* or other data needed to fill the list */ )
- {
- SOME_STUCT *new, *p;
-
- if ( ... )
- return NULL; /* have nothing to list */
-
- new = new_stuct();
-
- ... /* fill first member of list */
-
- p = new;
-
- while ( ... ) {
- p->next = new_stuct();
- p = p->next;
- ... /* fill subsequent member of list */
- }
-
- return new;
- }
-
-
- It's practically an idiom, and it runs a heck of a lot
- faster than you think on most hardware.
-
- --Blair
- "What's a 'stuct'?"
-