A simple understanding of variable parameter lists in C language

Source: Internet
Author: User

The number of arguments in a function prototype is fixed in general, but what if you want to receive an indefinite number of arguments at different times? The C language provides a variable list of parameters to implement.

Variable parameter lists are implemented by macros that are defined in the header file of the stdarg.h. The header file declares a va_list type and a va_start, Va_arg, va_end three macros. When we use the variable parameter list, we need to declare a variable of type va_list with these three macros.

Va_start (va_list variable name, the last named argument preceded by the ellipsis): This macro must be called before the variable parameter is extracted for initialization.

Va_arg (va_list variable name, Type_of_var): Used to extract the variable, Type_of_var is the type of the extracted variable. Returns the parameter of the corresponding type.

Va_end (va_list variable name): After the parameters have been processed, va_end must be called to do some cleanup.

The following example is excerpted from the C and pointers

#define _crt_secure_no_warnings#include<stdio.h> #include <stdlib.h> #include <stdarg.h> #include  <string.h>float average (int n_value,...)      The average value of the specified number of values {va_list var_arg; Declares the va_list variable int count = 0; float sum = 0;  Va_start (Var_arg, N_value);  Prepare to access the mutable parameter {for (count = 0; count < N_value; count++) {sum + = Va_arg (var_arg, int);   } va_end (Var_arg); Complete the processing of the variable parameters return sum/n_value; }}int Main () {printf ("%lf\n", average (6,1,2,3,4,5,6)); System ("pause"); return 0;}

The above example is a variable parameter list implementation that implements the average of the specified number of values.

Of course, there are many applications for variable parameters, such as the implementation of string copies

#define _crt_secure_no_warnings

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

void nstrcpy (char *dest, ...)
{
Va_list Pi; Declaring va_list variables
Char *p;
Va_start (pi, dest);
while ((P = va_arg (pi, char *)) = NULL)//by Va_arg (Pi,char *) to extract the variables in the argument list
{
strcpy (dest, p);
Dest + = strlen (p); Replication completes a variable for the next replication
}

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", a);
System ("pause");
return 0;
}

Completes a copy of multiple strings.

For a simple understanding of the variable parameter list, it is not clear what the three macro definitions are, and we will complete the blog about the mutable parameter list.

This article is from the "Aiali" blog, make sure to keep this source http://aliddd.blog.51cto.com/10780547/1710831

A simple understanding of variable parameter lists in C language

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.