home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18529 < prev    next >
Encoding:
Text File  |  1992-12-17  |  2.0 KB  |  88 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!cs.utexas.edu!sun-barr!ames!saimiri.primate.wisc.edu!hp9000.csc.cuhk.hk!hkuxb.hku.hk!h8915726
  3. From: h8915726@hkuxb.hku.hk (Medusa)
  4. Subject: Re: Variable argument list question
  5. Message-ID: <BzEqEC.MuI@hkuxb.hku.hk>
  6. Reply-To: h8915726@hkuxa.hku.hk
  7. Organization: University of Hong Kong
  8. X-Newsreader: Tin 1.1 PL4
  9. References: <1992Dec15.182906.25072@ugle.unit.no>
  10. Date: Thu, 17 Dec 1992 14:18:11 GMT
  11. Lines: 75
  12.  
  13. bjornw@colargol.edb.tih.no (Bjoern Wennberg) writes:
  14. : Hi!
  15. : I am trying to make a function that 
  16. :     1. Has an variable argument list
  17. :     2. Manipulates the arguments that the argument list points to
  18. : Here is an example which illustrates the problem:
  19. : int main(void)
  20. : {
  21. :   char *flag;
  22. :   func(flag);
  23. :   /* flag == "test" */
  24. : }
  25. : void func(...)
  26.     ^^^^^^^^
  27.     I'm not sure if it's gonna work, in PC, args should initially
  28.     point to the last _fixed_ argument. If there is not fixed argument,
  29.     I don't know. In fact, probably you may want to pass how many
  30.     parameters in the variable argument list.
  31.  
  32.     e.g. func (int n, ...)
  33.     
  34.     Otherwise, you never know when to stop. (Or you give a extra NULL as
  35.     terminating signal).
  36.  
  37. : {
  38. :   va_list args;
  39. :   va_start(args, "%s");
  40. :   
  41. :   va_arg(args, char *);
  42. :   
  43. :   /* allocate space for 'flag' by using 'args' */
  44. :   /* manipulate 'flag' by using 'args' */
  45. :   
  46. :   /* *args = new char[10];  doesn't work*/
  47. :   /* args = new char[10];  doesn't work*/
  48. :   /* memcpy() / strcpy() and friends doesn't work */
  49. :   /* 
  50. :    * debugger show me that 'args' points to the same address as 'flag'
  51. :    * does. But I cannot insert anything inside this address.
  52. :    */
  53. :   va_end(args);
  54. : }
  55. : Any comments are welcome.
  56. : bjornw>
  57. : --
  58. : bjornw@edb.tih.no     Bjoern Wennberg     Trondheim College of Engeneering
  59.  
  60.  
  61.     You can also try, directly, if you have:
  62.  
  63.     func (int n, ...)
  64.  
  65.     double    *f;
  66.  
  67.     f = &n;
  68.  
  69.     and then manipulate f...(but I think it's not very safe).
  70.  
  71.     Any comment welcome.
  72.  
  73.  
  74. --
  75. Medusa
  76. University of Hong Kong
  77. H8915726@hkuxa.hku.hk
  78.  
  79.