passing one argument to va_list function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool somefunc(void *in1, void *in2, ...)
{
  va_list arg;
  va_start(arg, in2);
  ...
  va_end(arg);
  return true;
}

// #1
somefunc(NULL, NULL, 1, 2, 3);
 
// #2
void *p = &some_int;
somefunc(NULL, NULL, "b", p, other_int, 1.1f);
 
// #3
somefunc(NULL, NULL, p3);


current limitation is only allow pass ONE variable "p3" to "somefunc"
in #3, how to pack or pass or do something to "p3", make the result same as #1 & #2?
or impossible to do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdarg.h>

void func(double d, ...)
{
        typedef char* pchar;

        va_list args;
        va_start(args, d);

        std::cout << "func(" << d << ", ";
        std::cout << (long)va_arg(args, long) << ", ";
        std::cout << (short)va_arg(args, int) << ", ";  // short passed as int
        std::cout << (int)va_arg(args, int) << ", ";
        std::cout << (const char*)va_arg(args, pchar) << ", ";
        std::cout << (char)(int)va_arg(args, int) << ");";      // char passed as int

        va_end(args);
}

int main()
{
        char ch = 'A';
        long l1 = 17;
        long l2 = 80;
        const char name[] = "Chairman Mo";
        int i = -2;

        func((double)3.2, (long)17, (short)80, (int)-2, "Chairman Mo", 'A');
        return 0;
}

Last edited on
as your reply

1
2
3
4
5
6
7
8
9
10
11
int main()
{
        char ch = 'A';
        long l1 = 17;
        long l2 = 80;
        const char name[] = "Chairman Mo";
        int i = -2;

        func((double)3.2, (long)17, (short)80, (int)-2, "Chairman Mo", 'A');
        return 0;
}


the limitation is, you only can pass ONE variable
1
2
3
4
5
6
7
8
/*

only_ony_variable contains (double)3.2, (long)17, (short)80, (int)-2, "Chairman Mo", 'A'
only_ony_variable can be a struct/point or other things, that contains all required value,


*/
func(only_ony_variable);
Sorry, I didn't think about it long enough. These are strong words, but I'm inlined to say you can't.

Variadic functions aren't really supported in C/C++.
May be this thread can help: http://www.cplusplus.com/forum/general/36157/

Regards
If you are using C instead of C++, the following thread may help:
http://stackoverflow.com/questions/479207/function-overloading-in-c

I like Christophs answer for C99.
It just occurred to me that you could do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool somefunc_wrapper(void *in1, void *in2, ...)
{
    va_list arg;
    va_start(arg, in2);
    bool result = somefunc(in1, in2, &arg);
    //or result = somefunc(in1, in2, arg); (see below)
    va_end(arg);
    return result;
}

int main()
{
    somefunc_wrapper(NULL, NULL, 1, 2, 3);
}

Here (depending on what you want to do) I would suggest the following options for somefunc:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool somefunc(void *in1, void *in2, ...)
{
    va_list arg;
    va_start(arg, in2);
    va_list* arg_in_arg = va_arg(arg, va_list*);
    //...
    va_end(arg);
    return true;
}

bool somefunc(void *in1, void *in2, va_list arg)
{
    //...
    return true;
}

bool somefunc(void *in1, void *in2, va_list& arg)
{
    //...
    return true;
}
Last edited on
Topic archived. No new replies allowed.