The processing of variable parameters of C-language function

Source: Internet
Author: User

/************************************************************************* > File name:va_list.c > Author:zs hh0604 > Mail: [email protected] > Created time:2014 October 14 Tuesday 15:16 09 seconds ********************************** /#include <stdio.h>/*** uses variable parameters in c, and C provides a variable number of va_list types to store Functions. 1. The variable parameter method for defining a function in the C language is as follows: one: the reference must be at the End.

Two: the last parameter must declare the type of the variable Parameter. For example, The following three: the ability to use a three-point Representation.

Four: the use of the function calls through the FMT this character parameter for the parameter type and the number of parameters Set. Such as: "%s%d%c%f" void Test (char *fmt,...); 2. Assume the use of variable Parameters.

C provides a set of functions such as the following: void Va_start (va_list ap, last), type va_arg (va_list ap, type), void va_end (va_list ap), void Va_copy (va_ List dest, Va_list src), which operates on a variable number of Parameters. function Prototypes. void Va_start (va_list ap,last); function function: points the AP to a variable number in Turn. function Parameters: ap: points to each variable parameter in Turn.

Last: is the fmt, and its role is to specify the number of variable parameters of the Parameter. and Types. function return Value: no return Value. You must first call the Va_start () function before calling the group Function. Formats the characters in the FMT format. To get the value of the parameter at Once.

Char *s;int d;char c;while (*fmt) {switch (*fmt++) {case ' s ': s = va_arg (ap, char *); break;case ' d ':d = va_arg (ap, int); Case ' C ': C = va_arg (ap, char); Va_end (ap); the Va_end () function must be called before the function ends.

Use vsnprintf () directly with ap.vsnprintf (): function prototypes such as the following: int vsnprintf (char *str, size_t size, const char *format, va_list ap); Use for example the Following://va_list function://header file <stdarg.h> provides functionality for traversing function tables of unknown data and Types. then, declare a variable ap of type va_list within function F. It will point to each actual argument in Turn. Va_list Ap;////you must initialize the AP once by using the Va_start macro, regardless of the unnamed number of References.

Va_start (va_list Ap,lastarg);////thereafter Each run of the macro va_arg will produce a value with the same type and value as the next Non-command parameter.

It also changes the AP at the same time so that the next parameter is returned the next time the va_arg is Run. Type Va_arg (va_list ap, type),////void ve_end (va_list ap),////after The processing of all processed parameters is Complete. And before exiting F. You must call the macro Va_end once.

void Va_end (va_list ap); **//************************************************************************* > Fil E name:snprintf.c > author:zshh0604 > Mail: [email protected] > Created time:2014 year October 14 Tuesday 13:21 13 sec * * * /#include <stdlib.h> #include < stdio.h> #include <stdarg.h>char * make_message (const char *fmt, ...) The {int n;/* is used to record the return of vsnprintf ().

Assuming this value is less than or equal to-1, the Lifespan. There is not enough space allocated.

Assuming that the value is greater than-1, but n is not less than size, then the value should be added 1, (size+1), otherwise space will be increased by one ***/int size = 100; /* Guess We need no more than bytes. */char *p, *np; Va_list ap;if ((p = malloc (size)) = = NULL) return null;while (1) {/* Try to print in the allocated SPACE. */VA_START (ap, FM t); This function allows the AP to point to each of the parameters in the variable parameter. n = vsnprintf (p, size, fmt, ap); Formats the number of APS that the AP points to in P.

The maximum size of P is sizes. print in FMT format.

Va_end (ap); After the use is finished, call Va_end (ap); Function.

/* If that worked, return the String. */if (n >-1 && N < size) return p;/* Else try again with more space. */if (n >-1)/* glibc 2.1 */size = n+1; /* precisely what is needed */else/* glibc 2.0 */size *= 2; /* twice the old size */if ((NP = realloc (p, Size) = = NULL) {free (p); return NULL;} Else {p = np;}}}


The processing of variable parameters of C-language function

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.