home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!ames!sun-barr!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Newsgroups: comp.lang.c
- Subject: Re: Multiple Parameter Passing (unspecified number)
- Message-ID: <1992Nov18.075350.1672@thunder.mcrcim.mcgill.edu>
- Date: 18 Nov 92 07:53:50 GMT
- References: <92321.124834GNR100@psuvm.psu.edu>
- Organization: McGill Research Centre for Intelligent Machines
- Lines: 57
-
- In article <92321.124834GNR100@psuvm.psu.edu>, <GNR100@psuvm.psu.edu> writes:
-
- > I'm looking to write a function that accepts any number of
- > paramerter. There will be at least two, an int and a char[]. The
- > rest will all be char[]'s. I need to know how this is done.
-
- In C Classic, <varargs.h>; in New C (ie, ANSI), <stdarg.h>. You will
- have to figure out how many arguments there are somehow - perhaps from
- the arguments you know are present, perhaps by having some magic value
- (nil pointer, for example) as the last argument or something....
-
- For example, if we assume that the first arg is the number of extra
- arguments, and the second arg is a buffer, and we're supposed to strcat
- all the strings together into the buffer, assuming the buffer is big
- enough:
-
- C Classic:
-
- #include <varargs.h>
-
- void strcatall(va_alist)
- va_dcl
- {
- va_list ap;
- int na;
- char *buf;
-
- va_start(ap);
- na = va_arg(ap,int);
- buf = va_arg(ap,char *);
- buf[0] = '\0';
- for (;na>0;na--) strcat(buf,va_arg(ap,char *));
- va_end(ap);
- }
-
- New C:
-
- #include <stdarg.h>
-
- void strcatall(int na, char *buf, ...)
- {
- va_list ap;
-
- va_start(ap,buf);
- buf[0] = '\0';
- for (;na>0;na--) strcat(buf,va_arg(ap,char *));
- va_end(ap);
- }
-
- (Of course, there are better ways to do this, like advancing buf and
- using strcpy...it's the varargs stuff I'm trying to illustrate, not
- string manipulation.)
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-