summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/varlist.c
blob: 5b6360b9d89bf4b864d0e6d152a630e986d32dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/************************************************************************
 *
 * Purpose: To show ... in action. This operator (?) allows us to pass
 *	    a variable number of parameters to a function, 'printf' 
 *	    uses ...
 * Author:  M.J. Leslie
 * Date:    09-Jul-94
 *
 ************************************************************************/

#include <stdio.h>
#include <stdarg.h>			/* va_list, va_arg, va_end	*/

int set(char *item, int, num, ...);

/************************************************************************/

main()
   {
   char *item="pear";

   (set (item,4, "apple", "pear", "banana", "grape") )
	? printf ("%s found\n", item) : printf("%s not found\n", item);

   }

/************************************************************************/

int set(char *item, int num, ...)
   {
   va_list ap;				/* define 'ap' It acts as a pointer 
					 * to the undefined variables.	*/
   int ok=0;
   int inc=0;
   va_start(ap, num);			/* seed 'ap'			*/

   do {
      if ( item == va_arg(ap, char *)) ok=1;
      } while ( ok==0 && ++inc < num);

   va_end(ap);				/* tidy up.			*/
   return (ok);
   }