Functions and macros that process variable numbers of optional parameters in C and C ++

Source: Internet
Author: User

Va Function Definition and VA macro

C language supports the VA function, which is an extension of C language. c ++ also supports the VA function, but it is not recommended in C ++.
The polymorphism introduced by C ++ can also be used to implement functions with variable parameter numbers. However, C ++ has completed the reload function.
It can only be a limited number of foreseeable parameters. In comparison, the VA function in C can define an infinite number.
C ++ is powerless in this respect. The advantages of VA functions are shown in
Simplicity and ease of use can simplify the code. C compiler in order to unify
The implementation on the platform and increased code portability provide a series of macros to shield the hardware environment from being different.
Difference.

In ansi c, VA macros are defined in stdarg. H. They include: va_list, va_start (), va_ar
G (), va_end ().

// Example 2: Calculate the sum of squares of any natural number:

Int sqsum (INT N1 ,...)

{

Va_list arg_ptr;

Int nsqsum = 0, n = N1;

Va_start (arg_ptr, N1 );

While (n> 0)

{

Nsqsum + = (N * n );

N = va_arg (arg_ptr, INT );

}

Va_end (arg_ptr );

Return nsqsum;

}

// Call time

Int nsqsum = sqsum (7, 2, 7, 11,-1 );

 

The prototype declaration format of variable parameter functions is:

Type vafunction (type arg1, type arg2 ,... );

Parameters can be divided into two parts: fixed parameters with fixed numbers and optional parameters with variable numbers. The function must at least
A fixed parameter. The declaration of a fixed parameter is the same as that of a common function. The number of optional parameters is unknown.
Use "... . The fixed parameters and optional parameters are the same as the parameter list of a function.

Let's take a look at the functions of each va_xxx using the simple example 2 above.

Va_list arg_ptr: defines a pointer to a variable number of parameter lists;

Va_start (arg_ptr, argn): sets the parameter list pointer arg_ptr to the first in the function parameter list.
Optional parameter. Description: argn is a fixed parameter located before the first optional parameter.
Fixed parameters ;... The order and sound of parameters in the function parameter list in the memory.
The order of time is the same. If the declaration of a va function is void va_test (char a, char B, c
Har C ,...), Then its fixed parameters are A, B, C, and the last fixed parameter argn is C. Therefore
Va_start (arg_ptr, c ).

Va_arg (arg_ptr, type): return the parameter specified by the pointer arg_ptr In the parameter list. The return type is Ty.
PE, and make the pointer arg_ptr point to the next parameter in the parameter list.

Va_copy (DEST, Src): the types of DeST and SRC are va_list. va_copy () is used to copy the parameter list.
Pointer to initialize DEST as SRC.

Va_end (arg_ptr): clears the parameter list. The parallel parameter pointer arg_ptr is invalid. Description: The pointer arg_pt.
After r is disabled, you can call va_start () and va_copy () to restore arg_ptr. Each call to va_st
After art ()/va_copy (), the corresponding va_end () must match. The parameter pointer can be in the parameter Column
The table is moved back and forth at will, but it must be in va_start ()... Within va_end.

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.