Content deleted Content added
Mindmatrix (talk | contribs) m Reverted edits by 2405:201:200D:1B50:EC44:12BC:B7D2:F3F7 (talk) to last version by Nickps |
Citation bot (talk | contribs) Add: date. | Use this bot. Report bugs. | Suggested by Headbomb | Linked from Wikipedia:WikiProject_Academic_Journals/Journals_cited_by_Wikipedia/Sandbox2 | #UCB_webform_linked 222/229 |
||
Line 49:
{{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 [[C2x|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.
|