Friday, July 31, 2009

How to allow unspecified number of parameters in C function?

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?

How to allow unspecified number of parameters in C function?
Use "..."





I don't really get it, but there's some documentation here: http://courses.cs.vt.edu/~cs1704/notes/A...
Reply: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 );


No comments:

Post a Comment