source: trunk/minix/man/man3/stdarg.3@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.0 KB
Line 
1.\" @(#)varargs.3 6.3 (Berkeley) 5/15/86
2.\"
3.TH STDARG 3 "May 15, 1986"
4.AT 3
5.SH NAME
6stdarg \- variable argument list
7.SH SYNOPSIS
8.nf
9.ft B
10#include <stdarg.h>
11
12void va_start(va_list \fIap\fP, \fIargtypeN\fP \fIparmN\fP)
13\fItype\fP va_arg(va_list \fIap\fP, \fItype\fP)
14void va_end(va_list \fIap\fP)
15.ft R
16.fi
17.SH DESCRIPTION
18This set of macros provides a means of writing portable procedures that
19accept variable argument lists.
20Routines having variable argument lists (such as
21.BR printf (3))
22that do not use
23.B stdarg
24are inherently nonportable, since different
25machines use different argument passing conventions.
26.PP
27A function that accepts a variable argument list is declared with "..." at
28the end of its parameter list. It must have at least one normal argument
29before the "...". For example:
30.PP
31.RS
32.nf
33int printf(const char *format, ...) { /* code */ }
34int fprintf(FILE *stream, const char *format, ...) { /* code */ }
35.fi
36.RE
37.PP
38.B va_list
39is a type which is used for the variable
40.I ap
41within the body of a variable argument function which is used to traverse
42the list.
43.PP
44.B va_start\c
45.RI ( ap ,
46.IR parmN )
47is called to initialize
48.I ap
49to the beginning of the list. The last true parameter of the function,
50.IR parmN ,
51must be supplied to allow
52.B va_start
53to compute the address of the first variable parameter.
54.PP
55.B va_arg\c
56.RI ( ap ,
57.IR type )
58will return the next argument in the list pointed to by
59.IR ap .
60.I Type
61is the type to which the expected argument will be converted
62when passed as an argument.
63.PP
64Different types can be mixed, but it is up
65to the routine to know what type of argument is
66expected, since it cannot be determined at runtime.
67.PP
68.B va_end\c
69.RI ( ap )
70must be used to finish up.
71.PP
72Multiple traversals, each bracketed by
73.B va_start
74\&...
75.B va_end,
76are possible.
77.SH EXAMPLE
78.nf
79.ta +4n +4n +4n +4n
80 \fB#include\fP <stdarg.h>
81.sp 0.4
82 execl(\fBconst char\fP *path, \fB...\fP)
83 {
84 \fBva_list\fP ap;
85 \fBchar\fP *args[100];
86 \fBint\fP argno = 0;
87
88 \fBva_start\fP(ap, path);
89 \fBwhile\fP ((args[argno++] = \fBva_arg\fP(ap, \fBchar\fP *)) != NULL) {}
90 \fBva_end\fP(ap);
91 \fBreturn\fP execv(path, args);
92 }
93.DT
94.fi
95.SH NOTES
96It is up to the calling routine to determine how many arguments
97there are, since it is not possible to determine this from the
98stack frame. For example,
99.B execl
100passes a null pointer to signal the end of the list.
101.B Printf
102can tell how many arguments are supposed to be there by the format.
103.PP
104The macros
105.B va_start
106and
107.B va_end
108may be arbitrarily complex;
109for example,
110.B va_start
111might contain an opening brace,
112which is closed by a matching brace in
113.BR va_end .
114Thus, they should only be used where they could
115be placed within a single complex statement.
116.SH BUGS
117It is impossible to properly show the macros as C declarations as is
118done in the synopsis. They can never be coded as C functions, because
119all three macros use their arguments by address, and the
120.I type
121field is certainly impossible.
122Just look at them as being part of the C language, like
123.BR sizeof .
Note: See TracBrowser for help on using the repository browser.