1, Introduction variadic function
A variable parameter function is a function that can change the number of parameters. e.g. printf ();
int printf (const char *format, ...);
printf ("%d%s\n", i,s);
C language can support variadic functions, one of the important reason is that the C call specification is specified in the C function call, the parameters are pressed from right to left into the stack, so a function implementation, there is no need to worry about calling his function will pass a few parameters, and as long as the care of their own use of several;
Example:
#include <stdarg.h>
void Print_int (int args,...)
{
Va_list ap; //va_list is used to save other arguments to the function.
Va_start (Ap,args); //Description where variable parameters start
for (int i=0;i<args;i++) {
printf ("Argument:%i\n",va_arg(ap,int));
}
Va_end (AP);
}
A series of macros are used here to help with variable parameter function parameters: Va_start, Va_arg, and va_end;
Under the ANSI C standard, these macros are defined in Stdarg.h. The three macros are prototyped as follows:
void Va_start (Va_list ap, last);//The pointer to the first mutable parameter (such as I in the above printf) to Ap,last is the final fixed parameter in the function declaration (such as *fromat in the printf function prototype);
Type Va_arg (va_list ap, type);//Returns the value of the variable parameter that the current AP points to, then the AP points to the next mutable parameter; Type represents the types of the current mutable parameters (supported type bits int and double);
void Va_end (va_list AP);//Set the AP to NULL
2, traps
Post a program that calculates a bill (from the Hi-C language), and the ABCD is the name of four different drinks. However, each guest may order only one, or a few drinks. So the parameters are variable.
enum Drink{a,b,c,d};// definition enum type
double Price (enum drink D) { //prices per beverage
switch (d) {
Case A:
return 6.79;
Case B:
return 5.31;
Case C:
return 4.82;
Case D:
return 5.89;
}
return 0;
}
double Total (int args,...) The core program calculates the total price.
{
double total=0;
va_list ap;
va_start (Ap,args);
int i;
for (i=0;i<args;i++) {
enum Drink D=va_arg (Ap,enum drink );//The problem is on this enum drink , It turns out to be an int, and of course the type mismatch also warns, but there's no way, that's where the flaw lies.
total+=price (d);
&NBSP;}
va_end (AP);
return total;
}&NBSP;
The problem is: at compile time, warning appears: ' Drink ' is promoted to (promoted to) ' int ' when passed through ' ... ' (so you should pass ' int ' not ' drink ' to ' V A_arg ')
If this code was reached, the program would abort.
And this program does not print out the total price, run crashes.
The answers in the book:
The 2nd parameter of the VA_ARG macro cannot be specified as a char, short, or float type. C and C + + classics: C Traps and defects when a variadic function is passed, the parameters of char and short type are promoted to type int, and the parameters of the float type are promoted to the double type.
A trap for variadic functions (variadic function)