C-language variable parameter va_list

Source: Internet
Author: User

#include <stdlib.h> #include <stdio.h> #include <stdarg.h>/*1.  use va_list va_start  Va_arg va_end realization of variable parameter */void simple_va_fun (int i,  ...)  {va_list arguments;int j = 0;int k, m;va_start (arguments, i); j =  va_arg (Arguments, int), M = va_arg (Arguments, int), va_end (arguments);p rintf ("%d,%d,%d \ n ", i,j,m);} /*2.  Fixed parameter function */void fixed_args_func (char x, int a, double b, char * c) {//print parameters in the stack address printf ("x = 0x%p\n",  &x);p rintf ("a = 0x%p\n",  &a);p rintf ( "B = 0x%p\n",  &b);p rintf ("c = 0x%p\n",  &c);} /*3.  implements its own mutable parameters, but actually takes the values of the parameters out of the stack in order */void var_args_func (const char * fmt,  ...)  {char* ap;ap =  (char *) &fmt + sizeof (FMT); int* int_ap =   (int *) ap;printf ("first:%d\n", *int_ap); int_ap = int_ap + 1;printf ("second:%d\n", *int_ap); Int_ap = int_ap + 1;// string start  indexchar* c_ptr =  (char*) int_ap;char* str =  (char* ) *int_ap;printf (" &int_ap = 0x%p\n ", Int_ap);p rintf (" &c_ptr  = 0x%p\n ",  c_ptr);p rintf (" &str    = 0x%p\n ", str);//stack is stored in the pointer to the string" HelloWorld ", that is, two-level character pointer printf (" third1:%s\n ", STR);p rintf ("third2:%s\n",  * (char **) int_ap);} Standard variable-parameter macro void std_vararg_func (CONST&NBSP;CHAR&NBSP;*FMT,&NBSP; ...) available in stdarg.h   {va_list ap;va_start (AP,&NBSP;FMT);p rintf ("%d\n",  va_arg (ap, int));p rintf ("%f\n",  va _arg (ap, double));p rintf ("%s\n",  va_arg (ap, char*)); Va_end (AP);} Int main ()  {//fixed_args_func (' A ',17, 5.40,  "Hello world");//char* p =   "Xiongwei";//printf ("%d\n", sizeof (p)); Var_args_func ("%d %d %s\n", 4, 5,  " HelloWorld ");p rintf ("--------------------------------------\ n "); Std_vararg_func ("%d %f %s\n ", 4, 5.4, " HelloWorld "); System (" pause "); return 0;}

Compared to the implementation of Std_vararg_func and Var_args_func, Va_list seems to be char*, Va_start seems to be ((char*) &fmt) + sizeof (FMT), Va_ Arg appears to be the first address of the next parameter. Yes, most of the platforms under Stdarg.h va_list, Va_start and VAR_ARG implementations are similar. General Stdarg.h will contain a lot of macros that look more complex. The implementation of stdarg.h in some systems relies on some special functions built into TheThe compilation system to handle variable argument lists and stack Allocations, the implementation of most other systems is similar to the following: (the Visual C + + 6.0 implementation is clearer because applications on Windows only need to be ported between Windows platforms, and there is no need to consider too many platform scenarios).

output://

First:4

Second:5

& int_ap = 0x003ef968

& c_ptr = 0x003ef968

& str = 0X013780D4

Third1:helloworld

Third2:helloworld

--------------------------------------

4

5.400000

HelloWorld


C-language variable parameter va_list

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.