Why use this???

Why use va_start, va_arg, and va_end in order to make a function accept a variable number of arguments?

Cant it be done with a simple pointer?

I tried but it cant. Can someone explain why?
Because parameter passing is not simple.

There are a number of ways to do it. A good starting read is at Wikipedia

The macros allow the compiler to properly decode where arguments are found in memory when calling a function.

In the simplest, basic case, they are pushed in order onto the stack. The macros can then take the address of the first argument (the required one), then do some pointer addition to find each additional argument. Again, exactly how this works depends on a lot of things, including compiler options. Hence the very special case of the macros.

Hope this helps.
closed account (Dy7SLyTq)
look up printf and scanf. it can be useful if your making something like a game engine and you need to save the state of an unknown number of variables

Ok is it also because it can contain anything from a char to a long long?
Yes. In order to calculate the address of the next argument you must know the size of the current.
@DTSCode that is exactly the opposite, the number must be known at compile time.
closed account (Dy7SLyTq)
no i mean like lets say i want to save location name and items. so i pass those to save function, then i later pass it ummmm all of those plus the fact that the player talked to a guy
That's a completely different and irrelevant scenario from "an unknown number of variables".
closed account (Dy7SLyTq)
i meant that you dont have to know the number of variables when writing the function
Topic archived. No new replies allowed.