A simple understanding of variable parameter lists in C Language

Source: Internet
Author: User

A simple understanding of variable parameter lists in C Language
In the function prototype, the number of parameters is usually fixed, but what if you want to receive parameters with an indefinite number at different times? The C language provides a variable parameter list. The variable parameter list is implemented through macros, which are defined in the header file of stdarg. h. The header file declares a va_list type and three macros, va_start, va_arg, and va_end. When using the variable parameter list, we need to declare a va_list type variable for use with these three macros. Va_start (va_list variable name, the last parameter with a name before the ellipsis): You must call this macro to initialize the variable parameters before extracting them. Va_arg (va_list variable name, type_of_var): used to extract the variable. type_of_var is the type of the extracted variable. Return parameters of the corresponding type. Va_end (va_list variable name): After processing the parameters, you must call va_end for cleanup. The following example is taken from c and pointer

# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include <stdarg. h> # include <string. h> float average (int n_value ,...) // calculate the average value of a specified number of values {va_list var_arg; // declare the va_list variable int count = 0; float sum = 0; va_start (var_arg, n_value ); // prepare the variable access parameter {for (count = 0; count <n_value; count ++) {sum + = va_arg (var_arg, int);} va_end (var_arg ); // complete Variable Parameter processing return sum/n_value;} int main () {printf ("% lf \ n", average (6, 1, 2, 3, 4, 5, 6 )); system ("pause"); return 0 ;}

 

The preceding example shows how to implement a variable parameter list that calculates the average value of a specified number of values. Of course, there are many applications for variable parameters, such as copying strings.
# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # include <stdarg. h> # include <string. h> void nstrcpy (char * dest ,...) {va_list pi; // declare the va_list variable char * p; va_start (pi, dest); while (p = va_arg (pi, char *))! = NULL) // extract the variable {strcpy (dest, p); dest + = strlen (p); In the parameter list through va_arg (pi, char ); // copy a variable to make the next copy} va_end (pi);} int main () {char a [100]; char * B = "asdg "; char * c = "qwewq"; char * d = "aswq"; nstrcpy (a, B, c, d); printf ("% s \ n", ); system ("pause"); return 0 ;}

 

Copies multiple strings. A simple understanding of the Variable Parameter List is not very clear about the specific definitions of the three macros, and a complete blog on the variable parameter list will be completed.

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.