[置顶] Java变参、C/C++/Objective_C变参


/*
	java中变参方法,变参即参数列表不固定,但是参数类型是一样的,在使用时,形参变量是数组类型的引用
*/

public class AppEnter{

	public static void main(String[] args) throws Exception{
	
		AppEnter.unfixedArguments("one", "two", "three");
		AppEnter.unfixedArguments(100, "one", "two", "three");

	}

	//方法中只有一个变参
	public static void unfixedArguments(String...arguments){

		System.out.println("形参最后的类型是:" + arguments.getClass().getSimpleName());

		for (String argument : arguments) {
			
			System.out.println(argument);
		}
	}

	//方法中有固定参数时,变参必须位于最右边,即作为参数列表的最后
	public static void unfixedArguments(int fixedArgument, String...arguments){

		System.out.println("形参最后的类型是:" + arguments.getClass().getSimpleName());

		for (String argument : arguments) {
			
			System.out.println(argument);
		}
	}
}	
	
///////////////////////////////
C/C++/Objective_C语言中函数参数往往都是固定的,调用时只需要传递对应类型的实参即可;但是有些情况,需要向函数传递不固定的参数。
比如:printf(),使用时,最少一个参数,最多不限。

在头文件中是这样说明的它的:
int printf(const char *format, ...);
后面的三个点...表示printf参数个数是不定的. 

在介绍编写变参函数前,先了解下stdarg.h在几个相关的函数:

void va_start(va_list ap, last);  
type va_arg(va_list ap, type);  
void va_end(va_list ap);  

其中:  
va_list是用于存放参数列表的数据结构。  
va_start函数根据last参数的相关信息,来初始化参数列表。  
va_arg函数用于从参数列表中取出一个参数,参数类型由type指定。  
va_end函数执行清理参数列表的工作。 

系统给的帮助说明如下:
va_start()
       The  va_start() macro initializes ap for subsequent use by va_arg() and
       va_end(), and must be called first.
       The argument last is the name of the last argument before the  variable
       argument list, that is, the last argument of which the calling function
       knows the type.
       Because the address of this argument may  be  used  in  the  va_start()
       macro,  it should not be declared as a register variable, or as a func‐
       tion or an array type.
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.
va_end()
       Each invocation of va_start() must be matched by a corresponding  invo‐
       cation of va_end() in the same function.  After the call va_end(ap) the
       variable ap is undefined.  Multiple traversals of the list, each brack‐
       eted  by va_start() and va_end() are possible.  va_end() may be a macro
       or a function.

EXAMPLES
     The function foo takes a string of format characters and prints out the
     argument associated with each format character based on the type.

           void foo(char *fmt, ...)
           {
                   va_list ap, ap2;
                   int d;
                   char c, *s;

                   va_start(ap, fmt);
                   va_copy(ap2, ap);
                   while (*fmt)
                           switch(*fmt++) {
                           case 's':                       /* string */
                                   s = va_arg(ap, char *);
                                   printf("string %s\n", s);
                                   break;
                           case 'd':                       /* int */
                                   d = va_arg(ap, int);
                                   printf("int %d\n", d);
                                   break;
                           case 'c':                       /* char */
                                   /* Note: char is promoted to int. */
                                   c = va_arg(ap, int);
                                   printf("char %c\n", c);
                                   break;
                           }
                   va_end(ap);
                   ...
                   /* use ap2 to iterate over the arguments again */
                   ...
                   va_end(ap2);
           }

说明:
va_start(ap, fmt);用于根据fmt初始化可变参数列表。
va_arg(ap, char *);用于从参数列表中取出一个参数,其中的char *用于指定所取的参数的类型为字符串。每次调用va_arg后,参数列表ap都会被更改,以使得下次调用时能得到下一个参数。
va_end(ap);用于对参数列表进行一些清理工作。调用完va_end后,ap便不再有效。
以上程序给了我们一个实现printf函数的是思路,即:通过调用va_start函数,来得到参数列表,然后我们一个个取出参数来进行输出即可。

例如:对于printf(“a=%d,b=%s,c=%c”,a,b,c)语句;fmt的值为a=%d,b=%s,c=%c,调用va_start函数将参数a,b,c存入了ap中。注意到:fmt中的%为特殊字符,紧跟%后的参数指明了参数类型. 

下面我们自定义一个打印函数,实现printf()的变参功能:
#include <stdarg.h>
void myprintf(char *fmt, ...)
{
    va_list ap;
    //基本类型:int double char char*
    int d;
    double f;
    char c;
    char *s;
    char flag;
    va_start(ap,fmt);
    while (*fmt){
        flag=*fmt++;
        if(flag!='%'){
            putchar(flag);
            continue;
        }
    flag=*fmt++;//记得后移一位
        switch (flag)
        {
            case 's':
                s=va_arg(ap,char*);
                printf("%s",s);
                break;
            case 'd': /* int */
                d = va_arg(ap, int);
                printf("%d", d);
                break;
            case 'f': /* double*/
                f = va_arg(ap,double);
                printf("%f", f);
                break;
            case 'c': /* char*/
                c = (char)va_arg(ap,int);        
                printf("%c", c);        
                break;
            default:
                putchar(flag);
                break;
        }   
    }
    va_end(ap);
}

int main(int argc, const char * argv[]) {
    // insert code here...
    myprintf("One is %d, Two is %s", 99, "ABC");
    
    return 0;
}

myprintf变参数函数的编写,必须要传入一个参数fmt,用来告诉我们的函数怎样去确定参数的个数。我们的可变参数函数是通过自己解析这个参数来确定函数参数个数及类型的。

下面我们编写一个求和函数,其函数实现如下: 
int sum(int cnt,...)
{
    int sum=0;
	int i;
    va_list ap;
    va_start(ap,cnt);
	for(i=0;i<cnt;++i){

		sum+=va_arg(ap,int);
	}
    
    va_end(ap);
	
	return sum;
} 

sum变参函数的编写,必须要传入一个参数cnt,用来告诉我们的函数体有多少个参数传入,这样for循环就知道循环多少次了。其实仔细思考下,如果参数cnt
不表示变参个数的话,那如何知道变参个数呢?办法就是,利用一个特殊值作为变参的最后一个传入,这个特殊值就意味着的变参的结束。

int sumOther(int first,...)
{
    int sum = first;

    va_list ap;
    va_start(ap,first);
    
    int temp = 0;
    while ((temp = va_arg(ap,int)) != 0) {
        
        sum += temp;
    }
    
    va_end(ap);
    
    return sum;
}

总结一下就是:通过va_start初始化参数列表(也就能得到具体的参数个数了,当然是间接的知道),然后使用va_arg函数从参数列表中取出你想要的参数,最后调用va_end执行清理工作。 




你可能感兴趣的:(stdarg,变参)