Possible Duplicate:
C/C++: Passing variable number of arguments around
According to the function declaration manner
return-type function-name(parameter-list, ...) {body ...}
Is the following code a kind of overloading?
(in A_FILE.h)
typedef VOID *FUNCTION(UINTN Number, ...);
It seems that I could just pass one Number parameter, or more parameters into the FUNCTION function, so does the number of parameters depends on the function implementation?
The ellipsis within a function declaration means that it will accept a number of arguments otherwise, variable parameters that are unknown at run-time, which by using the standard header file
stdarg.h, the respective functions in that header file ‘stdarg.h’ can determine what each of the variable parameters are that makes up the argument passing into a function.Consider this code example:
Typical invocation can be one of example:
Will produce an output on console in this manner:
Notice the usage of how the functions
va_start(...),va_end(...)and not to mentionvsprintf(...)will take care of filling in the blanks within the “unknown” parameters providedva_listis initialized to point to the variable parameters which are unknown at run-time.Edit: Just to emphasize, the invocation assumes that the string parameter in the form of a C string format is less than the maximum size represented by
PANIC_BUF_LENin the above example, nit-picky aside, that is to illustrate how a function can take in a number of standard C formatting strings used, for example, one can specify%din the string format, and expect aintto match up the parameter.