summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/var_func.c
blob: 4784d926ba3a07160a98f520235813401ff35516 (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
44
45
46
47
48
49
50
51
52
53
54
55
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);
}