Variable Parameter Functions

Source: Internet
Author: User

Http://blog.csdn.net/ithzhang/article/details/7021317

A variable-length function is a function with variable numbers and variable parameter types.

The most common examples are printf, scanf, and format functions in advanced languages. In C/C ++, to notify the compiler of variable parameter numbers and types (that is, indefinite and unknown), the Declaration of the function must end at three points.

// Printf function declaration int printf (const char * _ format ,...); // scanf function declaration int scanf (const char * _ format ,...); // The Declaration of the custom Variable Length Parameter Function func int func (int A, int B ,...);

In C/C ++, any function declared using the variable length parameter must have at least one specified parameter (also known as a mandatory parameter), that is, the type of at least one parameter is known, however, all parameter settings cannot be omitted using three vertices, and known specified parameters must be declared at the leftmost end of the function.

// The following statement is invalid int func (...); // The error int func (..., int A); // Error

  

  1. Because the C language stack pressure sequence is from right to left.
  2. Stack is extended downward, so the bottom of the stack is a high address, and the top of the stack is a low address.

    For example, if f (a, B, c, d) presses the stack from right to left, D should be the first to go to the stack, and a is the last to go to the stack, so the address of D should be higher than that of. So we can get the next parameter ,,

Therefore, parameters are adjacent to each other. Knowing the type and address of the previous parameter, you can obtain the content of the next parameter based on the type of the latter parameter.

 

 

 

// Access the variable parameter process va_list ARGs; // define a variable parameter list va_start (ARGs, ARG); // initialize ARGs to point to the next parameter of the mandatory parameter ARG; va_arg (ARGs, type); // get the content of the current parameter and point ARGs to the next parameter... // obtain all variable parameter content in a loop va_end (ARGs); // release ARGs

  

// Sum is the sum function, and its parameter type is int, but the number of parameters is not fixed // The first parameter (mandatory parameter) N specifies the number of variable parameters following int sum (unsigned int N ,...) {int sum = 0; va_list ARGs; va_start (ARGs, n); While (n> 0) {// pass va_arg (ARGs, INT) obtain the value sum + = va_arg (ARGs, INT); n --;} va_end (ARGs); Return sum ;}

  

The above code can be output normally after testing. That is to say, we use pointers to implement functions with variable parameters. But there is another problem here, that is, all the parameters of the sum function are of the int type. We know in advance that we want to move the pointer of the sizeof (INT) bit, but what if the parameter type is different?

Answer and analysis: this is indeed a troublesome problem, because the number of bytes occupied by different data types may be different (for example, the double type is 8 characters, short int type is 2), so it is difficult to determine how many bytes should be moved in advance! But there are still some methods. This is to use pointers. No matter what type of pointer, it occupies 4 bytes. Therefore, you can set all input parameters as pointers, in this way, we can traverse variable parameters by moving a fixed 4 bytes. Of course, we cannot know how to obtain and use the content in the pointer. So this is probably why functions like printf () and scanf () still need a format controller.

Variable Parameter Functions

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.