码迷,mamicode.com
首页 > 编程语言 > 详细

C语言之函数可变参数

时间:2014-05-30 17:51:46      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

先上一段代码:

bubuko.com,布布扣
#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(\n);
    /*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.


bubuko.com,布布扣

 

C语言之函数可变参数,布布扣,bubuko.com

C语言之函数可变参数

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/makefile/p/3760232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!