for some reason, i cant get this working:
void examplefunctionname(string str, ...){
...
va_start(ap, str.c_str());
nor do i get this work:
void examplefunctionname(string str, ...){
...
int len = str.length();
char *strlol = new char[len+1];
for(int i = 0; i < len; i++){
strlol[i] = str[i];
}
strlol[len] = 0;
va_start(ap, strlol);
but this does:
void examplefunctionname(const char *str, ...){
...
va_start(ap, str);
could someone show me how i can use string instead of const char *
there?
its outputting random numbers when i call examplefunctionname("%d %d %d", 1337, 1337, 1337)
Description. The C library macro void va_start (va_list ap, last_arg) initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis. This macro must be called before using va_arg and va_end.
The C library macro void va_start (va_list ap, last_arg) initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis. This macro must be called before using va_arg and va_end.
The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis. This macro must be called before using va_arg and va_end. Following is the declaration for va_start () macro. ap − This is the object of va_list and it will hold the information needed to retrieve the additional arguments with va_arg.
Each call to va_arg() modifies apso that the next call returns the next argument. The argument typeis a type name specified so that the type of a pointer to an object that has the specified type can be obtained simply by adding a * to type. The first use of the va_arg() macro after that of the va_start() macro returns the argument after last.
From the documentation:
va_start(va_list ap, last)
... The parameterlast
is the name of the last parameter before the variable argument list, i.e., the last parameter of which the calling function knows the type.
You've done this correctly in your working example: va_start(ap, str)
, and str
is the last known argument. But in the other two examples, you're passing odd things to va_start
.
va_start requires the previous parameter. That means that you have to pass in str directly, regardless of it's type. It does not take a const char* and it does not parse the string for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With