Implement a variable number of variables with C language

zhaozj2021-02-17  53

Implement a variable number of variables with C language

When using C language programming, the number of form parameters in the function is usually determined, and all actual parameters corresponding to the form parameters should be given in turn in turn. However, in some cases, it is desirable to be determined as needed. Typical examples have a familiar function Printf (), scanf () and system calling execl (), etc. So how do they achieve? The C compiler typically provides a series of macros that handle this situation, which is different from the different hardware platform, and increase the portability of the program. These macros include VA-START, VA-ARG, and VA-End. With these macros, there are two different forms. The two are different in the program included in the program, and there are some differences in the definition of macros. The declaration of the header files and macros of these two ways is shown in Table 1. When using the ANSI standard form, the prototype state of the parameter number variable is: Type FuncName (Type Para1, Type Para2, ...) This form requires at least one common form parameter, and the subsequent number of posts will not be omitted, and Is part of the function prototype, TYPE is the type of function return value and form parameters. When using the Unix System V compatible declaration, the number of variable variable variables is: Type FuncName (VA-ALIST) VA-DCL This form does not need to provide any common form parameters, TYPE is the type of function return value. . The VA-DCL is a detailed declaration of the parameter VA-ALIST in the function prototype declaration. It is actually a macro definition, which is defined by different types of different hardware platforms, but in the end, a semicolon is included, so the VA-DCL No need to add a semicolon. VA-DCL must be given in the code, and VA-ALIST can be given as it can be given in the VC, or it can be omitted, but it is omitted in the GCC on the Unix on the Unix. In addition, programs written by header files stdarg.h are in accordance with ANSI standards, which can be run on various operating systems and hardware, while using header file varargs.h is only compatible with previous programs. So I suggest that you will use the former. The basic principles of the two ways are consistent, but there are some subtle differences in the form of grammar. The following mainly describes the processing of parameters in the previous way. VA-START allows ArgP to point to the first optional parameter. VA-Arg Returns the current parameter in the parameter list and causes ArgP to point to the next parameter in the parameter list. VA-END seizes the ArgP pointer as NULL. These parameters can be traversed multiple times in the function, but they must start with VA-START and end with VA-END. When the caller is actually invoking the number of variable numbers, the caller should specify the number of actual parameters through a certain method, such as setting the last parameter as an empty string (system call execl () is like this), - 1 or Other ways. A specific example is given below, the previous part is a code that meets the ANSI standard, and the latter part is the code with the UNIX System V compatible method. Add some comments in the code, this is no longer explained here. This example has been compiled and running in VC / Windows NT4.0, CC / AIX4.3.2.0, GCC / Redhat Linux 6.0.

转载请注明原文地址:https://www.9cbs.com/read-29819.html

New Post(0)