4

In a function with variable arguments, we initialize an object of type va_list ,'ap' with the function va_start() as:

void va_start(va_list ap, parmN);

I don't understand

1.what type of objects can be passed as parMN(last known parameter). I've done with examples of passing integers, strings with format specifiers, structures etc.

2. How the parMN describes the following optional parameters.

4
  • 1
    See the C11 standard. In short: 1. va_start is a macro; macros have no concept of type; they work their magic with source text; 2. the following optional parameters are described with va_arg (also a macro) Commented Jul 15, 2019 at 9:24
  • 1
    Some light reading (examples are at the bottom). Bookmark that site, btw, for all your future C and C++ needs. Commented Jul 15, 2019 at 9:27
  • 1
    I've checked it already. Couldnt understand.. "macros have no concept of type;" that must be the point. Commented Jul 15, 2019 at 9:27
  • 2
    Indeed. They're about straight up substitution more than anything else. Commented Jul 15, 2019 at 9:28

1 Answer 1

7

The C standard says that va_start() is actually a macro, not a function, so it can do things a function couldn't. The details are highly implementation dependent but you could imagine that it takes the address of parmN to determine the stack address of the next parameter. There's no need for va_start() to know about the types of the following parameters because that information is passed to va_arg(), which is another macro.

Sign up to request clarification or add additional context in comments.

2 Comments

now i understand. I have been missing that va_start() is a macro ,not a function.
@KewalTakhellambam — the va_start() and va_arg() macros have to be macros because the second argument is the name of a type, and you can't pass type names to functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.