Use of variable parameter functions in C Language

Source: Internet
Author: User

Since function Overloading is not available in C, it is troublesome to solve the problem of variable number of function parameters. The problem of implementing an indefinite Parameter Function in C language is solved by using the stack used to call the function. The principle is to read the value of optional parameters through the stack until the set end mark is read. The end mark of the optional parameters of the following program is set to the optional parameter value smaller than 0.


In the include file of VC ++ 6.0, there is an stdarg. h header file, which has the following macro definitions:
# Define _ INTSIZEOF (n) (sizeof (n) + sizeof (int)-1 )&~ (Sizeof (int)-1 ))
# Define va_start (ap, v) (ap = (va_list) & v + _ INTSIZEOF (v) // The first optional parameter address
# Define va_arg (ap, t) (* (t *) (ap + = _ INTSIZEOF (t)-_ INTSIZEOF (t ))) // return the value at the ap address, and the ap points to the address of the next optional parameter.
# Define va_end (ap) (ap = (va_list) 0) // set the pointer to invalid
[Cpp]
# Include "stdafx. h"
# Include "stdarg. h"
Void test (int I, int j ,...)
{
Int data;
Va_list args; // defines the va_list variable.
Printf ("% d", I, j );
Va_start (args, j); // sets args to the first optional parameter after j.
While (data = va_arg (args, int)> = 0) // The va_arg function returns the variable value corresponding to the args pointer parameter, at the same time, args points to the next optional parameter <SPAN style = "WHITE-SPACE: pre"> </SPAN> // number. when the parameter is greater than or equal to 0, the data is read and printed. When you read <SPAN style = "WHITE-SPACE: pre"> </SPAN> // to a parameter smaller than 0, the loop ends.
Printf ("% d", data );
Printf ("\ n ");
Va_end (args); // make The args pointer invalid

}
 
 
Int main (int argc, char * argv [])
{
Test (0, 1, 3,-1 );
Return 0;
}

# Include "stdafx. h"
# Include "stdarg. h"
Void test (int I, int j ,...)
{
Int data;
Va_list args; // defines the va_list variable.
Printf ("% d", I, j );
Va_start (args, j); // sets args to the first optional parameter after j.
While (data = va_arg (args, int)> = 0) // The va_arg function returns the variable value corresponding to the args pointer parameter, at the same time, args points to the next optional parameter // number. when the parameter is greater than or equal to 0, the data is read and printed. When the value of read // is smaller than 0, the cycle ends.
Printf ("% d", data );
Printf ("\ n ");
Va_end (args); // make The args pointer invalid
 
}


Int main (int argc, char * argv [])
{
Test (0, 1, 3,-1 );
Return 0;
} [Cpp] view plaincopyprint? // The output result of the program is 0 1 3.

// The output result of the program is 0 1 3 [cpp] view plaincopyprint? /*
Main calls the test function stack distribution such:
 
Address: High ...............
| ...............
| The second optional parameter
| The first optional parameter
| J
| I
Low test return address
 
*/

/*
Main calls the test function stack distribution such:

Address: High ...............
| ...............
| The second optional parameter
| The first optional parameter
| J
| I
Low test return address

*/

 


Share:

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.