Java variable parameter, c/c++/objective_c variable parameter

Source: Internet
Author: User

In/*java, the parameter list is not fixed, but the parameter type is the same, and when used, the formal parameter variable is the reference of the array type */public class Appenter{public static void Main (string[] args) Throws Exception{appenter.unfixedarguments ("one", "one", "one", "three"); Appenter.unfixedarguments ("One", "one", "one", "three"); There is only one parameter in the method public static void Unfixedarguments (string...arguments) {System.out.println (the final type of the parameter is: "+ Arguments.getclass (). Getsimplename ()); for (String argument:arguments) {System.out.println (argument);}} When there are fixed parameters in the method, the argument must be on the far right, that is, the last public static void unfixedarguments (int fixedargument, string...arguments) {as the argument list) { System.out.println (the final type of the parameter is: "+ arguments.getclass (). Getsimplename ()); for (String argument:arguments) { System.out.println (argument);}}} The function parameters in the C/c++/objective_c language are often fixed, and only the arguments of the corresponding type need to be passed in the invocation, but in some cases it is necessary to pass an invariant argument to the function. For example: printf (), when used, at least one parameter, no limit. In the header file it is described in this way: int printf (const char *format, ...); The following three points ... Indicates that the number of printf parameters is variable.  Before introducing the variable parameter function, we first understand the following stdarg.h in several related functions: void Va_start (Va_list ap, last);  Type Va_arg (va_list ap, type); void Va_end(Va_list AP);  Where: Va_list is the data structure used to hold the parameter list.  The Va_start function initializes the parameter list based on information about the last parameter.  The Va_arg function is used to remove a parameter from the parameter list, and the parameter type is specified by type. The Va_end function performs cleanup of the parameter list. The help provided by the system is as follows: Va_start () the Va_start () macro Initializes AP for subsequent use by Va_arg () and Va_end (), and Mu       St be called first.  The argument last was the name of the last argument before the variable argument list, which was, the last argument of       which the calling function knows the type. Because the address of this argument is used in the Va_start () macro, it should is declared as a Regis ter variable, or as a func‐tion or an array type.va_arg () the va_arg () macro expands to an expression that ha  s 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, the next call returns the next argument. The argument type is a type name specified so and the typeof a pointer to an object that have 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.       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 promote       D according to the default argument promotions), random errors would occur. If AP is passed to a function, uses Va_arg (Ap,type) then the value of AP is undefined after the return of the Function.va_end () Each invocation of Va_start () must is matched by a corresponding invo‐cation of va_end () I  n 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 () is 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 char           Acter 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 ':/* Strin                                   G */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           ); Description: Va_start (AP, FMT), used to initialize a mutable parameter list based on FMT. Va_arg (AP, char *); used to remove a parameter from the parameter list, where char * is used to specify the type of the parameter being taken as a string. Each time the va_arg is called, the parameter list AP is changed so that the next parameter can be obtained the next time the call is made. Va_end (AP); Used for some cleanup of the parameter list. After the va_end is called, the AP is no longer valid. The above procedure gives us a way to implement the printf function, that is, by calling the Va_start function to get the parameter list, and then we take out the parameters to output. For example: for printf ("a=%d,b=%s,c=%c", a,b,c) statements, the value of FMT is a=%d,b=%s,c=%c, and the Va_start function is called to deposit the parameter a,b,c into the AP. Note that the% in the FMT is a special character, and the parameters that follow the% indicate the parameter type. Here we customize a print function to implement the variable function of printf (): #include <stdarg.h>void myprintf (char *fmt, ...)    {va_list ap;    Base type: 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++;//Remember to move back one 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, and the other is%s", and "ABC"); return 0;} To write a myprintf variable parameter function, you must pass in a parameter fmt to tell us how the function determines the number of arguments. Our variadic function determines the number and type of function parameters by parsing the parameter itself. Here we write a summation function whose function is implemented as follows: 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;} The sum argument function must be written in a parameter CNT, which tells us how many arguments the function body has passed in, so that the for loop knows how many times the loop is. In fact, if the parameter CNT does not represent the number of variables, how do you know the number of arguments? The idea is to use a special value as the last pass of the argument, and this special value means the end of the argument. 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;}  To summarize: Initialize the parameter list by Va_start (you can get the exact number of arguments, of course indirectly), and then use the Va_arg function to take the arguments you want from the argument list and finally call Va_end to perform the cleanup.


Java variable parameter, c/c++/objective_c variable parameter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.