Variable parameter list va_list C language

1. va_list basic principles and usage:

#include <stdio.h> 
#include <stdarg.h>
 void FUNC ( int I, char * CH, ...) {     // the format 
    the va_list AP; 
    the va_start (AP, CH);               // points to a certain parameter, from start 
    char * STR; 
    STR = CH;
     do { 
        the printf ( " % S " , STR); 
        STR = the va_arg (AP, char *);      // need to specify the parameters of type char * type, inflexible 
    } the while (STR); 
} 
int main () 
{   
    FUNC (1,"A","B",NULL);
    return 0;
}

2. va_list and vsprintf used in conjunction with:

#include<stdio.h>
#include<stdarg.h>
void func(int i,char *fmt,...){     //format
    va_list ap;
    va_start(ap,fmt);
    char str[20];
    vsprintf(str, fmt, ap);
    va_end(ap);
    printf("%s\n",str);
}
int main()
{  
    char str1[]="str1";
    char str2[]="str2";
    int d=1000;
    func(1,"%s %s %d",str1,str2,d);
    return 0;
}

This is exactly the principle of printf.

Extended: Modern C ++ in initializer_list.

 

Guess you like

Origin www.cnblogs.com/abnk/p/11224090.html