tags:

views:

226

answers:

2

I have following function with only a variable parameter list (Without supporting/fixed argument). Is it possible to get values passed to this function? I am using ANSI format.

Foo( ... )
{
}

Adding some more points for clarity. In my special case, the number of arguments and their types are stored in a .xml file as configurations which can be accessed from Foo().

Edit:

I am adding some information from my trial and error. I partially succeeded to get data from list as follows.

va_list Arguments;

Arguments = (( va_list ) &Arguments + Offset );

Data = va_arg( Arguments, Type );

Idea I put behind this is directly reading data from stack. Here the problem factor is Offset whose value is varying according to the number of arguments I am passing into Foo(). For example, when I am passing a pointer only, I put its value as 16 to get correct result. I found this values with trial and error. I want to derive Offset from parameter list that my program can run without any runtime failure in all possible cases. Can someone tell me the relation between this Offset and the parameter list.

I am using Visual Studio 2008 for developing this.

+2  A: 

Variadic functions with no fixed arguments are not supported in modern C. However, for backwards compatibility purposes, they are supported by varargs.h. See also this man page, which provides the following example code. Note the K&R-style function definition required by varargs.

#include <varargs.h>
void foo(va_alist) va_dcl {
    va_list ap;
    va_start(ap);
    while(...) {
        ...
        x = va_arg(ap, type);
        ...
    }
    va_end(ap);
}
Josh Kelley
+1  A: 

Variadic functions require fixed arguments in modern C because functions should be able to sort out what to do from the arguments. For instance, printf() knows what arguments to retrieve from the stack based on the format string. If you omit these fixed parameters, there is no checking on the correctness of the arguments.

If Foo() performs a lookup of some kind, then it would make sense to pass either xml reference or the values to look up as a parameter (or better: both).

Going back to K&R style C is quite nasty. Possibly you can change the splitting between parser and interpreter Foo() to use a more clear interface.

Adriaan
Thanks; Actually I am not in search of something to pass because I should pass something. I am trying to solve it without adding any redundancy or dummy information.
Vadakkumpadath
If your function needs some global data (like a xml reference) then it would make sense to me to use this (this would not be redundant, but improve (lower) coupling within your program.
Adriaan