diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2008-01-27 11:37:44 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@xenon.tklauser.home> | 2008-01-27 11:37:44 +0100 |
commit | 7e0f021a9aec35fd8e6725e87e3313b101d26f5e (patch) | |
tree | b1cacc4b24393f517aeb4610e9e1021f954307a8 /reference/C/EXAMPLES/varlist.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/EXAMPLES/varlist.c')
-rw-r--r-- | reference/C/EXAMPLES/varlist.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/varlist.c b/reference/C/EXAMPLES/varlist.c new file mode 100644 index 0000000..5b6360b --- /dev/null +++ b/reference/C/EXAMPLES/varlist.c @@ -0,0 +1,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); + } |