How to use string in va_start?

for some reason I can't get this to work:

void examplefunctionname(string str, ...){ ... va_start(ap, str.c_str()); 

and I do not 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 it does:

  void examplefunctionname(const char *str, ...){ ... va_start(ap, str); 

can someone show me how i can use string instead of const char * there?

the output of its random numbers when calling examplefunctionname("%d %d %d", 1337, 1337, 1337)

+6
c ++
source share
4 answers

From the documentation:

va_start(va_list ap, last) ... last parameter is the name of the last parameter before the variable argument list, i.e. The last parameter that the calling function knows the type.

You did it right in your working example: va_start(ap, str) , and str is the last known argument. But in the other two examples, you pass the weird stuff to va_start .

+5
source share

va_start requires the previous parameter. This means that you must pass right in the line, regardless of type. It does not accept const char * and does not parse the string for you.

+1
source share

va_start is a macro that uses the second argument as the location, so you need to use the last example that you give.

0
source share
 void examplefunctionname(string str, ...){ ... va_start(ap, str); 
0
source share

All Articles