Interpretation of c standard library source code (vc9.0) -- stdarg. h

Source: Internet
Author: User
Tags vc9

Stdarg. h defines the method for processing variable parameter functions. Some of our commonly used functions, such as printf and scanf, are used for processing. They are rarely used when writing code. However, we need to learn one more method to understand more usage methods. In fact, the implementation of variable parameters is found by pushing the parameters into the stack during compilation. For proper processing, the corresponding structure type needs to be given, because the data value needs to be retrieved Based on the structure size. It is not difficult to understand and write functions that process indefinite parameters.

Variable Arguments handling

This header defines macros to access the individual arguments of a list of unnamed arguments whose number and types are not known to the called function.

A function may accept a varying number of additional arguments without corresponding parameter declarations by including a comma and three dots (,...) After its regular named parameters:

Return_type function_name (parameter_declarations ,...);
To access these additional arguments the macros
Va_start, va_arg and
Va_end, declared in this header, can be used:

  • First, va_start initializes the list of variable arguments as ava_list.
  • Subsequent executions of va_arg yield the values of the additional arguments in the same order as passed to the function.
  • Finally, va_end shall be executed before the function returns.
Types
Va_list
Type to hold information about variable arguments (type)
Macro Functions
Va_start
Initialize a variable argument list (macro)
Va_arg
Retrieve next argument (macro)
Va_end
End using variable argument list (macro)
Va_copy
Copy variable argument list (macro)

 

#include <stdarg.h>#include <assert.h>/* type definitions */typedef struct {char c;} Cstruct;int tryit(char * format,...){int ctr = 0;va_list ap;va_start(ap,format);while (*format){switch(*format){case 'i' :assert (va_arg(ap, int) == ++ctr);break;case 'd' :assert (va_arg(ap, double) == ++ctr) ;break;case 'p' :assert (va_arg(ap, char *)[0] == ++ctr);break;case 's' :assert (va_arg(ap, Cstruct).c == ++ctr);}}va_end(ap);return ctr;}int main (){Cstruct stu;stu.c = 'a';int k = tryit("dip",1.0,10,"str",stu);return 0;}

 

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.