C language variable parameter function __ function

Source: Internet
Author: User

The C language has a function to implement variable parameters is the use of placeholders, the method is to use the following way to define the function void frees (int size, ...). The rest of the place is the same as the general function definition, except that the second argument uses three points instead of the generic variable type variable name. You can use this function to handle variable variables as long as you use this definition. But the definition of light is not enough, but also need to use the following two functions a type definition, in fact, these functions are implemented through macros.

Va_list

Va_start ();

Va_arg ();

Va_list, which is a type definition, we can interpret it as a pointer to the address of the first parameter. If we define this: Va_list AP, then the AP is a variable that points to the first parameter in all parameters. It differs from the general pointer variable, which is a composite variable. That is, it can be found by this variable, in which the function is called the other variable passed in. But we can not directly point to the next variable by moving the pointer. The rest of the work will go through the following two methods.

Va_start (ap,size), the AP points to the first parameter size and can get the type int of size. At this point, if you have the next argument, you need to have the AP refer to an argument down and get its type.

Va_arg (AP, type) so that the AP points to an argument and can get the type of that parameter. Attention. The type is very important, learn the pointer should be clear, the type of the pointer if wrong, the correct location, the number can be taken out of the mess.

Here is a function to release a series of memory spaces using this feature of the C language:

void frees (int size, ...) {
	va_list ap;
	int i=0;
	Va_start (AP, size);
	For (i=0 i<size; i++) {
		void* ptr = Va_arg (AP, void*);
		Free2 (PTR);
	}

In the use of time only to indicate the number of space to be freed, as well as their value, you can.

Reference: Simple depth of functions in C language, three point function


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.