-3

I have this:

long int addsquares(int n, ...) 

How can I access the parameters? I can't use va_start and va_arg...

simonc
  • 41,632
  • 12
  • 85
  • 103
Tomás Francisco
  • 791
  • 3
  • 7
  • 15

4 Answers4

1

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.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • As homework says, I have to access the stack to know the the address of argument n... I don't know how... – Tomás Francisco Apr 08 '13 at 12:46
  • inline assembly language is your friend – K Scott Piel Apr 08 '13 at 12:53
  • @PTF - If you look at the source for the `va_xxx` macros, you'll see it's very system dependent (depends on your architecture and compiler at least), When a function call is made the parameters get pushed onto the stack, you can see this if you look at the disassembly. What's your env? – Mike Apr 08 '13 at 12:57
1

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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • How can I access to eatch parameter with p[i]? I can't with p[1] or p[2] for exemple... – Tomás Francisco Apr 08 '13 at 15:03
  • @PTF You tried to run a pre-test code? and call function eg) `addsquares(3, 1,2,3);` There is a need to look at pre-test-code on your system. After this manner is not exactly a portable way. – BLUEPIXY Apr 08 '13 at 15:10
  • Stack frame can be expected parameter is said to be in sequence but depends on the implementation, you can predict the address of the following placement If you know the difference between the position of the address of the following parameters: the address of the first parameter is obtained can. – BLUEPIXY Apr 08 '13 at 15:25
  • @PTF What happened to you the results(output of pointers address) of running the pre-test code? – BLUEPIXY Apr 08 '13 at 15:35
  • Execution example: http://ideone.com/J3WuOv , but this method may not go well anytime. – BLUEPIXY Apr 08 '13 at 16:07
  • Is that answer, I don't know why the people vote this question negatively. Thank you so much for your time and patience – Tomás Francisco Apr 11 '13 at 20:05
  • In this case was 4 at 4. I access the first paramenter with a *pointer, and I have to increment 4 to the pointer to access the second, and 4 again to the next, and the same for the followings. This happen because the int type occupy 4 bytes, and this parameters is accessible in the stack 4 at 4 to the int type. – Tomás Francisco Apr 11 '13 at 20:13
0

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.

jalgames
  • 781
  • 4
  • 23
0

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.

Community
  • 1
  • 1
K Scott Piel
  • 4,320
  • 14
  • 19