📄 stdarg.3
字号:
.\" @(#)varargs.3 6.3 (Berkeley) 5/15/86.\".TH STDARG 3 "May 15, 1986".AT 3.SH NAMEstdarg \- variable argument list.SH SYNOPSIS.nf.ft B#include <stdarg.h>void va_start(va_list \fIap\fP, \fIargtypeN\fP \fIparmN\fP)\fItype\fP va_arg(va_list \fIap\fP, \fItype\fP)void va_end(va_list \fIap\fP).ft R.fi.SH DESCRIPTIONThis set of macros provides a means of writing portable procedures thataccept variable argument lists.Routines having variable argument lists (such as.BR printf (3))that do not use.B stdargare inherently nonportable, since differentmachines use different argument passing conventions..PPA function that accepts a variable argument list is declared with "..." atthe end of its parameter list. It must have at least one normal argumentbefore the "...". For example:.PP.RS.nfint printf(const char *format, ...) { /* code */ }int fprintf(FILE *stream, const char *format, ...) { /* code */ }.fi.RE.PP.B va_listis a type which is used for the variable.I apwithin the body of a variable argument function which is used to traversethe list..PP.B va_start\c.RI ( ap ,.IR parmN )is called to initialize.I apto the beginning of the list. The last true parameter of the function,.IR parmN ,must be supplied to allow.B va_startto compute the address of the first variable parameter..PP.B va_arg\c.RI ( ap ,.IR type )will return the next argument in the list pointed to by.IR ap ..I Typeis the type to which the expected argument will be convertedwhen passed as an argument..PPDifferent types can be mixed, but it is upto the routine to know what type of argument isexpected, since it cannot be determined at runtime..PP.B va_end\c.RI ( ap )must be used to finish up..PPMultiple traversals, each bracketed by.B va_start\&....B va_end,are possible..SH EXAMPLE.nf.ta +4n +4n +4n +4n \fB#include\fP <stdarg.h>.sp 0.4 execl(\fBconst char\fP *path, \fB...\fP) { \fBva_list\fP ap; \fBchar\fP *args[100]; \fBint\fP argno = 0; \fBva_start\fP(ap, path); \fBwhile\fP ((args[argno++] = \fBva_arg\fP(ap, \fBchar\fP *)) != NULL) {} \fBva_end\fP(ap); \fBreturn\fP execv(path, args); }.DT.fi.SH NOTESIt is up to the calling routine to determine how many argumentsthere are, since it is not possible to determine this from thestack frame. For example,.B execlpasses a null pointer to signal the end of the list..B Printfcan tell how many arguments are supposed to be there by the format..PPThe macros.B va_startand.B va_endmay be arbitrarily complex;for example,.B va_startmight contain an opening brace,which is closed by a matching brace in.BR va_end .Thus, they should only be used where they couldbe placed within a single complex statement..SH BUGSIt is impossible to properly show the macros as C declarations as isdone in the synopsis. They can never be coded as C functions, becauseall three macros use their arguments by address, and the.I typefield is certainly impossible.Just look at them as being part of the C language, like.BR sizeof .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -