I have this:
long int addsquares(int n, ...)
How can I access the parameters?
I can't use va_start
and va_arg
...
I have this:
long int addsquares(int n, ...)
How can I access the parameters?
I can't use va_start
and va_arg
...
If you are saying that you have a variadic function, and you're not allowed to use the variable argument macros (va_xxx
) then you'd have to rewrite the content of those macro's yourself.
Unless you can change the function prototype, but I'm guessing that's now allowed either.
Implementation dependent...
pre test
long int addsquares(int n, int d1, ...){
printf("%p,%p\n", &n, &d1);
return 0L;
}
result : windows 64bit system, vc10 (sizeof int:4)
003DFD54,003DFD58
windows 64bit system, gcc 4.4.3 (sizeof int:4)
000000000022FE60,000000000022FE68
for vc10:
long int addsquares(int n, ...){
int i, *p = &n;
long sum = 0L;
for(i=1;i<=n;++i)
sum += p[i]*p[i];
return sum;
}
for gcc:
long int addsquares(int n, ...){
int i, *p = &n;
long sum = 0L;
for(i=1;i<=n;++i)
sum += p[i*2]*p[i*2];
return sum;
}
Use Arrays and store each parameter in one "cell" of the array.
long int addsquares(int[] parameters)
{
for (int i = 0; i < parameters.length(); i++)
{
//Use current parameter: parameters[i]
}
}
It's c# code, but i thinkit will work for c as well.
Check out the discussion in this thread... How does the C compiler implement functions with Variable numbers of arguments?
I think you'll find it gets you going in the right direction. Pay particular attention to the discussion on the need to use one of the arguments as a means to sort out what and where the other arguments are.