C-language variable parameter list processing

Source: Internet
Author: User

The principle of transfer of function parameters
function parameters are accessed in memory in the form of stacks, from right to left into the stack.
The parameter stores the format in memory:
In the process, the stack address is allocated from high to low. When executing a function, the argument list is put into the stack, pushed into the high address portion of the stack, and then into the stack function's return address, and then into the stack function code execution, the stack address is constantly decreasing.
In summary, the function is distributed in the stack, where the address is from high to low, in order: function argument list, function return address, function execution code snippet. In the stack, the distribution of each function is flashback. That is, the last parameter is in the highest part of the list, and the first parameter is the lowest part of the list address. parameter in the stack.
The distribution is as follows: The last parameter, the second parameter,->...-> the first parameter, the function returns the address, the function code snippet.
Macro definition:
typedef char* VA_LIST
#define VA_START (Ap,v) (ap= (va_list) &v+_intsizeof (v))
#define VA_ARG (Ap,t) (* (t*) ((AP +=_intsizeof (t))-_intsizeof (t)))
#define VA_END (AP) (AP = (va_list) 0)
Va_list is a character pointer that can be understood as a pointer to the current parameter, which must be passed through this pointer. The following steps are called:
1. Before calling the parameter table, define a variable of type va_list (assuming that the Va_list type variable is defined as an AP);
2. The AP should then be initialized to point to the first parameter in the variable parameter table, which is implemented by Va_start, the first parameter is the AP itself, the second parameter is a variable next to the argument list, that is, "..." before the parameter;
3. Then get the parameter, call Va_arg, its first parameter is the AP, the second parameter is the specified type of the parameter to get, then return the value of the specified type, and point the position of the AP to the next variable position of the parameter list;
4. After getting all the parameters, it is necessary to turn off the AP pointer to avoid danger, by calling Va_end, which is the input parameter, the AP is set to NULL and should be used to close the pointer after getting the parameter table. To put it bluntly, is to make our program robust. Usually Va_start and va_end are paired up.

Features of individual macros:
Va_list is used to declare a variable, and we know that the variable argument list of a function is actually a string, so va_list is declared as a character pointer, which declares a character pointer variable that points to a list of arguments, for example: Va_list Ap;//ap: Arguement pointer
Va_start (AP,V) Its first argument is a variable that points to a mutable argument string, the second argument is the first parameter of a variadic function, and is typically used to specify the number of parameters in a mutable argument list.
Va_arg (AP,T) Its first argument points to a variable argument string, and the second argument is the type of the mutable parameter.
The Va_end (AP) is used to empty variables that hold variable parameter strings (assigned to null).
Usage of va_list:
(1) First define a variable of type va_list in the function, which is a pointer to the parameter
(2) Then use the Va_start macro to initialize the variable just defined by the va_list variable, the second parameter of the macro is the first variable parameter of the previous parameter, is a fixed parameter.
(3) Then return the variable parameter with Va_arg, the second parameter of Va_arg is the type of the parameter you want to return.
(4) Finally, the Va_end macro is used to end the variable parameter acquisition. Then you can use the second parameter in the function. If the function has more than one mutable parameter, call Va_arg in turn to get each parameter.
Va_list processing in the compiler:
(1) after running Va_start (AP, v), the AP points to the address of the first mutable parameter on the stack.
(2) Va_arg () Gets the variable parameter value of type T, in which first apt = sizeof (type T), the AP refers to the address of the next parameter. The T-type * pointer of the ap-sizeof (t type) is then returned, which is the address of the first mutable parameter in the stack. Then use * to get the contents of this address.
(3) Va_end (), the x86 platform is defined as AP = ((char*) 0), so that the AP no longer points to the stack, but as with NULL, some are directly defined as ((void*) 0), so the compiler does not generate code for va_end, such as This is how GCC is defined in the Linux x86 platform.

For example:

1. #include <stdio.h> #include <string.h> #include <stdarg.h>//ansi standard form of declaration, the ellipsis in parentheses indicates an optional parameter int demo ( Char *msg, ...)   {//define the structure of the save function parameter va_list ARGP;   int Argno = 0;  Char *para;   ARGP points to the first optional parameter passed in, MSG is the last determined parameter Va_start (ARGP, msg); while (1) {para = Va_arg (ARGP, int.), #类型不能为char, signed char, unsigned char, short, unsigned short, signed short, S           Hort int, signed short int, unsigned short int, float if (strcmp (Para, "") = = 0) break;           printf ("Parameter #%d is:%s\n", Argno, para);  argno++;  } va_end (ARGP); Set ARGP to null return 0;}   int main () {Demo ("Demo", "This", "is", "a", "demo!", ""); return 0;} 2. #include <stdio.h> #include <stdarg.h> #include <stdlib.h>//The first parameter specifies the number of arguments int sum (int numbers,...)  {va_list vaptr;  int i;  int sum = 0;  Va_start (Vaptr,number);  for (i=0; i<number;i++) {sum + = Va_arg (vaptr,int); #类型不能为char, signed char, unsigned char, short, unsigned short, signed short, short int, signed short int, unsigned short int, float} va_end (vaptr); return sum;}  int main () {printf ("%d\n", Sum (4,4,3,2,1)); return 0;}

Note:
1. Because Va_start, va_arg, va_end, etc. define macro, the type and number of variable parameters are completely controlled by the program code in this function, it is not able to intelligently recognize the number and type of different parameters, that is to say, If you want to implement intelligent identification of variable parameters, you have to do this by making judgments in your own programs.
2.va_arg (Ap,type) when a parameter is taken, the type must not be of the following types: Char, signed char, unsigned char, short, unsigned short, signed short, short int , signed short int, unsigned short int, float. In the C language, when invoking a function without a prototype declaration: The caller performs a "default real-parameter promotion (argument promotions) for each parameter." Also, the above promotion will be performed for each actual parameter after the variable-length parameter list exceeds the formal parameter of the last type declaration. The lifting work is as follows: The actual parameters of the A.float type are promoted to Double;b.char, short, and the corresponding signed, and the actual parameters of the unsigned type are promoted to int;c. If int cannot store the original value, it is promoted to the unsigned int. The caller then passes the promoted argument to the callee. Therefore, my_printf is absolutely unable to receive the actual parameters of the above type.


C-language variable parameter list processing

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.