Variadic Functions in C
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
void
formal parameter:
int max(int a, int b, ...)
max(10, 5, 13, 7, 9, -1)
char
array) to
provide information about the number of other parametersprintf("%d %d %d %d %d", 10, 5, 13, 7, 9)
<stdarg.h>
va_list
: Data typevoid va_start(va_list ap, type last_fixed)
: Initialize the argument pointertype va_arg(va_list ap, type)
: Return the next argument (and update ap
)void va_end(va_list ap)
: Clean upvoid va_copy(va_list destination, va_list source)
va_start()
then allocates memory and
va_end()
later frees memory