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/var_func.c |
Initial import (2.0.2-6)2.0.2-6
Diffstat (limited to 'reference/C/EXAMPLES/var_func.c')
-rw-r--r-- | reference/C/EXAMPLES/var_func.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/var_func.c b/reference/C/EXAMPLES/var_func.c new file mode 100644 index 0000000..4784d92 --- /dev/null +++ b/reference/C/EXAMPLES/var_func.c @@ -0,0 +1,56 @@ +/************************************************************************ + * + * Purpose: Program to demonstrate functions that have a variable + * number of parameters. + * Author: M J Leslie. + * Date: 28-Mar-94 + * + ************************************************************************/ + +#include <stdio.h> +#include <stdarg.h> /* va_list, va_arg, va_end */ + +int set(char *item, int num, ...); /* Declare the function. */ + +/************************************************************************/ + +main() +{ + char *item="pear"; + + int Ret; + + Ret = set (item,4, "apple", "pear", "banana", "grape"); + + if (Ret) + { + printf ("%s found\n", item); + } + else + { + 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 Ret=0; + int Inc=0; /* Assume the worst. */ + va_start(ap, num); /* seed 'ap' */ + + do + { + if ( item == va_arg(ap, char *)) + { + Ret = 1; + } + } while ( Ret==0 && ++Inc < num); + + va_end(ap); /* tidy up. */ + return (Ret); +} |