A simple understanding of variable parameters in C language-maximum value calculation

Source: Internet
Author: User

Variable parameters are a usage Technique in C. They are implemented using macros. In fact, they are mainly based on a variable parameter pointer, and then constantly move the pointer back.

However, it is easier to understand how to use it.


[Cpp]
# Include <stdarg. h> // <SPAN style = "COLOR: # ff0000"> header files required by variable parameters </SPAN>
# Include <stdio. h>
 
Int max (int count ,...){
Va_list ap; // 1) defines a variable parameter, which is actually a pointer used to access the parameter list in variable parameters.
Va_start (ap, count); // 2) initialize a variable parameter and point the defined pointer to the first parameter.
Int maximum =-1; // assume that all values are positive integers for the maximum value. If not, set maximum to the maximum negative number.
Int temp, I;
For (I = 0; I <count; I ++ ){
Temp = va_arg (ap, int); // obtain an int type parameter from the current position of the Variable Parameter List pointer, and point the pointer to the next parameter.
If (maximum <temp)
Maximum = temp;
}
Va_end (ap); // end
Return maximum;
}
 
Int main (int argc, char ** argv ){
Int ret_max = max (9, 1, 6, 21, 32, 5, 68, 15, 32, 62 );
Printf ("max in (9, 1, 6, 21, 32, 5, 68, 15, 32, 62) is % d", ret_max );
Return 0;
}

# Include <stdarg. h> // use the header file required by variable parameters
# Include <stdio. h>

Int max (int count ,...){
Va_list ap; // 1) defines a variable parameter, which is actually a pointer used to access the parameter list in variable parameters.
Va_start (ap, count); // 2) initialize a variable parameter and point the defined pointer to the first parameter.
Int maximum =-1; // assume that all values are positive integers for the maximum value. If not, set maximum to the maximum negative number.
Int temp, I;
For (I = 0; I <count; I ++ ){
Temp = va_arg (ap, int); // obtain an int type parameter from the current position of the Variable Parameter List pointer, and point the pointer to the next parameter.
If (maximum <temp)
Maximum = temp;
}
Va_end (ap); // end
Return maximum;
}

Int main (int argc, char ** argv ){
Int ret_max = max (9, 1, 6, 21, 32, 5, 68, 15, 32, 62 );
Printf ("max in (9, 1, 6, 21, 32, 5, 68, 15, 32, 62) is % d", ret_max );
Return 0;
}

 


Basically, variable parameters are used in the following steps:

Define a va_list variable before calling the parameter table. Va_list ap;

Initialize the ap and point it to the first parameter in the variable parameter table. This is achieved through va_start. The first parameter is the ap itself, the second parameter is a variable next to the variable in front of the variable parameter table. In this case, the ap can be directly used as a parameter for functions such as printf. For example, printf (msg, ap );

If you need to obtain the parameters yourself, you can call va_arg. This step is not required for many print businesses. Call va_arg. The first parameter is ap, the second parameter is the specified type of the parameter to be obtained, and then the value of the specified type is returned, point the ap location to the next variable location in the variable parameter table;

After obtaining all the parameters, it is necessary to turn off the ap pointer to avoid danger. The method is to call va_end. The input parameter ap is set to NULL, it is also a convention and international practice to disable pointers after obtaining the parameter table.


 

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.