C語言stdarg頭文件(stdarg.h)中va_copy宏的用法及代碼示例。
用法:
void va_copy (va_list dest, va_list src);
複製變量參數列表
要從中提取的下一個參數dest與將從中提取的內容相同src。
調用的函數va_copy,還應調用va_end在dest在它返回之前。
參數
返回值
空示例
/* va_copy example */
#include <stdio.h> /* printf, vprintf*/
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen, strcat */
#include <stdarg.h> /* va_list, va_start, va_copy, va_arg, va_end */
/* print ints until a zero is found: */
void PrintInts (int first,...)
{
char * buffer;
const char * format = "[%d] ";
int count = 0;
int val = first;
va_list vl,vl_count;
va_start(vl,first);
/* count number of arguments: */
va_copy(vl_count,vl);
while (val != 0) {
val=va_arg(vl_count,int);
++count;
}
va_end(vl_count);
/* allocate storage for format string: */
buffer = (char*) malloc (strlen(format)*count+1);
buffer[0]='\0';
/* generate format string: */
for (;count>0;--count) {
strcat (buffer,format);
}
/* print integers: */
printf (format,first);
vprintf (buffer,vl);
va_end(vl);
}
int main ()
{
PrintInts (10,20,30,40,50,0);
return 0;
}
輸出:
[10] [20] [30] [40] [50] [0] |
相關用法
注:本文由純淨天空篩選整理自C標準庫大神的英文原創作品 C va_copy function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。