C language uses the va_list, va_start, va_end, and va_arg macros to define variable parameter functions, va_listva_arg

Source: Internet
Author: User

C language uses the va_list, va_start, va_end, and va_arg macros to define variable parameter functions, va_listva_arg

Before defining a function with variable parameters, let's first understand the transfer principle of the function parameters:

1. function parameters are accessed using the stack data structure. In the function parameter list, the function parameters are imported from right to left.

2. Parameter memory storage format: the parameter memory address is stored in the stack segment of the memory. During function execution, the last (rightmost) parameter is input to the stack. Therefore, the stack bottom is high and the stack top is low. For example:

Void test (int a, float B, char c );

Then, when calling the test function, the real parameter char c first stack, float B, and int, therefore, the storage order of variables in the memory is c-> B-> a, Because theoretically, we only need to detect the address of any variable and know the type of other variables, by using the pointer shift operation, you can find other input variables.

To implement a Variable Parameter Function, you need to use the following macros:

Typedef char * va_list; // declares a void va_start (va_list ap, prev_param) pointer to the parameter list. // The first parameter is a character pointer variable pointing to a variable parameter, the second parameter is the first parameter of a Variable Parameter. It is usually used to specify the number of parameters in the Variable Parameter List void va_arg (va_list ap, type ); // The first parameter is a variable pointing to the variable parameter character pointer, and the second parameter is the variable parameter data type void va_end (va_list ap ); // empty the variable that stores the variable parameter string (value: NULL)
3. Example: Calculate the sum of N numbers

int sum(int count, ...){    int sum = 0;    int i;    va_list ap;    va_start(ap, count);    for (i = 0; i < count; ++i)    {        sum += va_arg(ap, int);    }    va_end(ap);    return sum;}
int main(int argc, const char * argv[]){    int ret = sum(5, 1, 2, 3, 4, 5);    printf("sum: %d\n",ret);}
4. Example: use of functions related to variable parameters in the standard library

Void test (int count ,...) {va_list ap; va_start (ap, count); vprintf ("% d, % d, % d \ n", ap ); // format the output variable parameter value char buff [1024]; vsprintf (buff, "a = % d, B = % d, c = % d \ n", ap ); // format the value of the Variable Parameter List and output it to the buffer zone printf ("% s \ n", buff); vfprintf (stdout, "a = % d, B = % d, c = % d \ n ", ap); // print the value of the variable parameter list to the standard output. // The variable parameter is passed into the int type address vsscanf (", 30, 40 "," % d, % d, % d ", ap); // format the value of the string in sequence and input it to the variable vsnprintf (buff, 30, "a = % d, B = % d, c = % d", ap); // format the value of the Variable Parameter List and output the specified length (30 characters) ("vsnprintf = % s \ n", buff); va_end (ap );}

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.