Question:
How to allow unspecified number of parameters in C function?
mdigitale
2006-04-18 13:35:50 UTC
For example, in C you have the function "printf" which takes a char* string with text, formatters, etc. and then you pass in a comma delimited list of variables for each parameter you provided in the first string. How do you define such a function? How do you make reference to these parameters? I assume there must be some base pointer which can be used to access the various entries?
Three answers:
Arbitrage
2006-04-18 13:41:40 UTC
Use "..."



I don't really get it, but there's some documentation here: http://courses.cs.vt.edu/~cs1704/notes/A02.AdvancedFunctionParameters.pdf
?
2016-12-13 15:11:55 UTC
A reference parameter has an ampersand (&) before the call of the argument (and after the form). So for an int: void somefunction(int &arg1); or once you're making use of the STL in a sort interface report: void somefunction(std::string &str); it may desire to be argued that reference is a factor of the form, yet syntactically, i think that it makes issues much less annoying to study to place it final. case in point, a reference argument of form pointer to vector of floats: void somefunction(std::vectore8cd7da078a8672631ad64f35f5a6c0* &array1);
VarmintHunter07
2006-04-18 14:40:58 UTC
Use ellipses in function definition:



void Foo( ... )



then use the following to get arguments:



va_list marker;

int first = va_arg( marker, int );

char* pSecond = va_arg( marker, char* );

...

va_end( marker );


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...