Variable Arguments

Posted By: TWO

Variable Arguments - 10/03/07 16:02

JCL once postet a piece of code from a standard C book. With some fiddling I erased all compiler errors, but it produces a crash. So, any chance to get some variable args into my app?
Posted By: TWO

Re: Variable Arguments - 10/06/07 16:11

Boo is making some Foo.
Posted By: TWO

Re: Variable Arguments - 10/07/07 09:22

bump bump bump
Posted By: Excessus

Re: Variable Arguments - 10/07/07 10:17

Are you talking about

int main(int argc, char *argv[])

or

void func(int, ...);
Posted By: TripleX

Re: Variable Arguments - 10/07/07 10:22

example for variable arguments:

Code:

#include <cstdarg>
#include <iostream>

using namespace std;

double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;

va_start ( arguments, num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.
va_end ( arguments ); // Cleans up the list

return sum / num; // Returns some number (typecast prevents truncation)
}
int main()
{
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}


Posted By: TWO

Re: Variable Arguments - 10/07/07 11:14

As I'm skilled in Cpp I know how to use them there I want a Lite-C sample cause I never got it to work there.

Referring to this thread: http://www.coniserver.net/ubbthreads/sho...true#Post733811
Posted By: Excessus

Re: Variable Arguments - 10/07/07 14:23

Seems like it simply doesn't work. The arguments aren't copied to the stack in case of "...".

I wrote a little program that demonstrates it. Both functions tst1 and tst2 should return the integer that is stored after parameter a. This seems to work only for tst1.

Code:

#include <acknex.h>
int tst1(int a, int b)
{
return *((&a) + 1);
}
int tst2(int a, ...)
{
return *((&a) + 1);
}

int main()
{
if(tst1(1, 2) == 2)
printf("test 1 success");
else
printf("test 1 failure");

if(tst2(1, 2) == 2)
printf("test 2 success");
else
printf("test 2 failure");
}


Posted By: Ottawa

Re: Variable Arguments - 10/08/07 23:05

Hi!

Was int not changed to integer?

Ottawa
Posted By: Uhrwerk

Re: Variable Arguments - 10/08/07 23:56

JCL stated, that this is not supported at the moment, because it required major changes in the way the compiler works. However, he stated he'd take care about this in the future.
© 2024 lite-C Forums