varargs.5

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 5 代码 · 共 131 行

5
131
字号
.TH varargs 5 RISC.SH Namevarargs \- handle variable argument list.SH Syntax.B #include <varargs.h>.PP.B va_alist.PP.B va_dcl.PP.B void va_start(pvar).br.B va_list pvar;.PP.B \fItype\fB va_arg(pvar, \fItype\fB).br.B va_list pvar;.PP.B void va_end(pvar).br.B va_list pvar;.SH DescriptionThis set of macros allows portable procedures that accept variableargument lists to be written.Routines that have variable argument lists, such as.MS printf 3s ,but that do not use .PN varargsare inherently nonportable, as differentmachines use different argument-passing conventions..PP.TP 10.B va_alistIs used as the parameter list in a function header..TP 10.B va_dclIs a declaration for .IR va_alist .A semicolon should not follow.IR va_dcl ..TP 10.B va_listIs a type defined for the variableused to traverse the list..TP 10.B va_startIs called to initialize.I pvarto the beginning of the list..TP 10.B va_argReturns the next argument in the listpointed to by.IR pvar ..I TypeIs the type the argument is expected to be.Different types can be mixed, but it is upto the routine to know what type of argument isexpected. This information cannot be determined at run time..TP 10.B va_endis used to clean up..PPMultiple traversals, each bracketed by.I va_start\&....IR va_end ,are possible..PPThe calling routine must specify how many argumentsthere are, because it is not always possible to determine this from thestack frame.  For example,.PN execlis passed a zero pointer to signal the end of the list.  The.PN printfroutine can tell how many arguments there are by the format..PPIt is nonportable to specify a second argument of.IR char ,.IR short ,or.IR floatto .IR va_arg ,because arguments seen by the called function are not.IR char ,.IR short ,or.IR float .C converts .I charand .I shortarguments to .I intandconverts.I floatarguments to.IR double before passing them to a function..SH ExamplesThe following example presents an implementation of .MS execl 2 :.PP.nf	#include <varargs.h>	#define MAXARGS	100	/\(**	execl is called by			execl(file, arg1, arg2, ..., (char \(**)0);	\(**/	execl(va_alist)	va_dcl	{		va_list ap;		char \(**file;		char \(**args[MAXARGS];		int argno = 0;		va_start(ap);		file = va_arg(ap, char \(**);		while ((args[argno++] = va_arg(ap, char \(**)) != (char \(**)0)			;		va_end(ap);		return execv(file, args);	}.fi.SH See Alsoexec(2), printf(3s), vprintf(3s)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?