Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use string in va_start?

Tags:

c++

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)

like image 232
Newbie Avatar asked Jan 10 '11 15:01

Newbie


People also ask

What is va_start and va_end in C?

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.

How to use va_arg and va_end macros in C?

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.

How to retrieve additional arguments with va_arg in a function?

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.

How do I use 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.


2 Answers

From the documentation:

va_start(va_list ap, last) ... The parameter last 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.

like image 153
Cascabel Avatar answered Oct 27 '22 02:10

Cascabel


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.

like image 33
Puppy Avatar answered Oct 27 '22 01:10

Puppy