home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18346 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  1.7 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!asuvax!chnews!sedona!bhoughto
  2. From: bhoughto@sedona.intel.com (Blair P. Houghton)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help!!!!!!!!!!!!
  5. Date: 14 Dec 1992 21:40:49 GMT
  6. Organization: Intel Corp., Chandler, Arizona
  7. Lines: 68
  8. Message-ID: <1giv11INNptr@chnews.intel.com>
  9. References: <1992Dec12.184235.29056@sbcs.sunysb.edu> <1992Dec14.174931.4650@rexago8.uucp>
  10. NNTP-Posting-Host: alfalfa.intel.com
  11.  
  12. In article <1992Dec14.174931.4650@rexago8.uucp> aa@rexago8.uucp (Adam Andrews) writes:
  13. >jcontro@engws10.ic.sunysb.edu () writes:
  14. >>   SOME_STUCT  *Ptr;
  15. >>fn( ..., Ptr );
  16. >
  17. >Try passing it like: fn( ..., &Ptr);
  18. >and receiving it like: fn ( SOME_STRUCT **Ptr) { ...
  19.  
  20. Try having fn() create the list from whole cloth
  21. and return the list:
  22.  
  23.     typedef struct ... SOME_STUCT;
  24.  
  25.     ...
  26.     {
  27.     SOME_STUCT  *Ptr;
  28.     ...
  29.     Ptr = fn();            /* gimme da list */
  30.     ...
  31.     }
  32.  
  33.     /* just a little OOP */
  34.     SOME_STUCT  *new_stuct( void )
  35.     {
  36.     SOME_STUCT  *new;
  37.  
  38.     new = (SOME_STUCT  *) malloc ( sizeof(SOME_STUCT) );
  39.     if ( !new ) {
  40.         ...                /* barf and die */
  41.     }
  42.  
  43.     new->foo = NULL;        /* zero members appropriately */
  44.     new->bar = 0;
  45.     new->bazz = 0UL;
  46.     ...
  47.     new->next = new->prev = NULL;
  48.  
  49.     return new;
  50.     }
  51.  
  52.     SOME_STUCT  *fn( void /* or other data needed to fill the list */ )
  53.     {
  54.     SOME_STUCT  *new, *p;
  55.  
  56.     if ( ... )
  57.         return NULL;        /* have nothing to list */
  58.  
  59.     new = new_stuct();
  60.  
  61.     ...                /* fill first member of list */
  62.  
  63.     p = new;
  64.  
  65.     while ( ... ) {
  66.         p->next = new_stuct();
  67.         p = p->next;
  68.         ...                /* fill subsequent member of list */
  69.     }
  70.  
  71.     return new;
  72.     }
  73.  
  74.  
  75. It's practically an idiom, and it runs a heck of a lot
  76. faster than you think on most hardware.
  77.  
  78.                 --Blair
  79.                   "What's a 'stuct'?"
  80.