C言語の関数可変パラメータ


前のコード:
#include<cstdarg> #include<iostream> #include<string>

using namespace std; void error(char* format,...){//        ,   ...       va_list ap; int d,flag; char c, *s; va_start(ap,*format);// args            ,va_start,va_end   

    while (*format){ switch (*format) { case 's':              /* string */

                   if(!flag) break; s = va_arg(ap, char *); cout<<s;flag=0; break; case 'd':              /* int */

                    if(!flag) break; d = va_arg(ap, int); cout<<d;flag=0; break; case 'c':              /* char */

                   /* need a cast here since va_arg only takes fully promoted types */

                   if(!flag) break; c = (char) va_arg(ap, int); cout<<c;flag=0; break; case '%': flag=1; break; default: putchar(*format); } format++; } putchar('
'); /*for(i=0;i<args;i++){// //printf("%d",va_arg(ap,int)); }*/ va_end(ap);//destroy va_list; } int main(){ char s[]="rich"; error("I am %s,I have %d %c",s,100,'$'); //getchar(); return 0; }
error() printf 。
#include <stdarg.h>

void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);
va_arg , ap , man :
va_arg()
The va_arg() macro expands to an expression that has the type and value
of the next argument in the call. The argument ap is the va_list ap
initialized by va_start(). Each call to va_arg() modifies ap so that
the next call returns the next argument. The argument type is a type
name specified so that the type of a pointer to an object that has the
specified type can be obtained simply by adding a * to type.

The first use of the va_arg() macro after that of the va_start() macro
returns the argument after last. Successive invocations return the
values of the remaining arguments.

If there is no next argument, or if type is not compatible with the
type of the actual next argument (as promoted according to the default
argument promotions), random errors will occur.

If ap is passed to a function that uses va_arg(ap,type) then the value
of ap is undefined after the return of that function.