Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===In C=== To portably implement variadic functions in the [[C (programming language)|C language]], the standard [[stdarg.h|{{code|stdarg.h}}]] header file is used. The older [[varargs.h|{{code|varargs.h}}]] header has been [[Deprecation|deprecated]] in favor of {{code|stdarg.h}}. In C++, the header file {{code|cstdarg}} is used.<ref>{{cite web|url=http://www.cplusplus.com/reference/clibrary/cstdarg/|title=<cstdarg> (stdarg.h) - C++ Reference|website=www.cplusplus.com}}</ref> <syntaxhighlight lang="C"> #include <stdarg.h> #include <stdio.h> double average(int count, ...) { va_list ap; int j; double sum = 0; va_start(ap, count); /* Before C23: Requires the last fixed parameter (to get the address) */ for (j = 0; j < count; j++) { sum += va_arg(ap, int); /* Increments ap to the next argument. */ } va_end(ap); return sum / count; } int main(int argc, char const *argv[]) { printf("%f\n", average(3, 1, 2, 3)); return 0; } </syntaxhighlight> This will compute the average of an arbitrary number of arguments. Note that the function does not know the number of arguments or their types. The above function expects that the types will be {{code|int}}, and that the number of arguments is passed in the first argument (this is a frequent usage but by no means enforced by the language or compiler). In some other cases, for example [[printf]], the number and types of arguments are figured out from a format string. In both cases, this depends on the programmer to supply the correct information. (Alternatively, a [[sentinel value]] like {{code|NULL}} or {{code|nullptr}} may be used to indicate the end of the parameter list.) If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the [[format string attack]]. Depending on the system, even using {{code|NULL}} as a sentinel may encounter such problems; {{code|nullptr}} or a dedicated null pointer of the correct target type may be used to avoid them. {{code|stdarg.h}} declares a type, {{code|va_list}}, and defines four macros: [[va start|{{code|va_start}}]], [[va arg|{{code|va_arg}}]], [[va copy|{{code|va_copy}}]], and [[va end|{{code|va_end}}]]. Each invocation of {{code|va_start}} and {{code|va_copy}} must be matched by a corresponding invocation of {{code|va_end}}. When working with variable arguments, a function normally declares a variable of type {{code|va_list}} ({{code|ap}} in the example) that will be manipulated by the macros. # {{code|va_start}} takes two arguments, a {{code|va_list}} object and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). In [[C23 (C standard revision)|C23]], the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.{{r|g=note|n=KandR|r=Making the named parameter optional was needed since there was no way to specify a function taking an unspecified number of arguments in C23 after the removal of K&R style function definitions. Since C++ was already using this syntax for the same purpose, this change was also a way to increase compatibility between the languages.<ref>{{cite web|url=https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu#n2975---relax-requirements-for-variadic-parameter-lists|title=C23 is Finished: Here is What is on the Menu Β§N2975 - Relax requirements for variadic parameter lists|date=31 July 2022 }}</ref>}}<ref name=N2975>{{cite web|url=https://open-std.org/JTC1/SC22/WG14/www/docs/n2975.pdf|title=WG14-N2975 : Relax requirements for variadic parameter lists, v3|date=2022-04-15|last1=Gilding|first1=Alex|last2=Meneide|first2=JeanHeyd}}</ref> It initialises the {{code|va_list}} object for use by {{code|va_arg}} or {{code|va_copy}}. The compiler will normally issue a warning if the reference is incorrect (e.g. a reference to a different parameter than the last one, or a reference to a wholly different object), but will not prevent compilation from completing normally. # {{code|va_arg}} takes two arguments, a {{code|va_list}} object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of {{code|va_arg}} allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument. # {{code|va_end}} takes one argument, a {{code|va_list}} object. It serves to clean up. If one wanted to, for instance, scan the variable arguments more than once, the programmer would re-initialise your {{code|va_list}} object by invoking {{code|va_end}} and then {{code|va_start}} again on it. # {{code|va_copy}} takes two arguments, both of them {{code|va_list}} objects. It clones the second (which must have been initialised) into the first. Going back to the "scan the variable arguments more than once" example, this could be achieved by invoking {{code|va_start}} on a first {{code|va_list}}, then using {{code|va_copy}} to clone it into a second {{code|va_list}}. After scanning the variable arguments a first time with {{code|va_arg}} and the first {{code|va_list}} (disposing of it with {{code|va_end}}), the programmer could scan the variable arguments a second time with {{code|va_arg}} and the second {{code|va_list}}. {{code|va_end}} needs to also be called on the cloned {{code|va_list}} before the containing function returns.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)