By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

some inner thoughts on va_start()

P: n/a
While using variable arguments we have to initialize variable argument
like

va_start( arg_ptr, prevParam );

Can any body explain what is the significance of second
parameter(prevParam) while initialization of variable arguments.
Following is an extract from msdn, i am not able to understand the
relevance of first parameter in the function Average() and how it is
used while initialization of variable arguments.

int main( void )
{
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
}

int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

Jun 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* Sekhar:
While using variable arguments we have to initialize variable argument
like

va_start( arg_ptr, prevParam );

Can any body explain what is the significance of second
parameter(prevParam) while initialization of variable arguments.


For stack-based argument passing it provides a known address on the
stack. From this address the address of the first ... argument can be
easily found. It all depends on the implementation.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 30 '06 #2

P: n/a
Sekhar wrote:
While using variable arguments we have to initialize variable argument
like

va_start( arg_ptr, prevParam );


The varargs functions were disgusting hacks added AFTER the language
was implemented. On the original PDP-11 implementations you could
just dig around on the stack to find the rest of the args so you didn't
really need any compiler support, but you did need at least one real
arg to take the address so you could start mining). Of course, when
C started to get more standardized, they reallized that RISC chips
and other argument passing would need real varargs compiler assistance,
they added the compiler syntax (such as the ... declaration). The
existing practice however wasn't much altered (although there are
slight differences between the original varargs.h and the now
standard stdarg.h macros).
Jun 30 '06 #3

P: n/a
Joe
The second parameter accomplishes two things. 1) It provides a handle
to the stack the arguments are on and 2) even if that were not
required, it lets the va_args stuff know where the variable arguments
start. That is if I had a function:

void args(int a, char * p, ...)

it's convenient to skip over a and p before processing the var args.

joe

Jun 30 '06 #4

P: n/a

Sekhar wrote:
While using variable arguments we have to initialize variable argument
like

va_start( arg_ptr, prevParam );

Can any body explain what is the significance of second
parameter(prevParam) while initialization of variable arguments.
Following is an extract from msdn, i am not able to understand the
relevance of first parameter in the function Average() and how it is
used while initialization of variable arguments.

int main( void )
{
printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );
}

int average( int first, ... )
{
int count = 0, sum = 0, i = first;
va_list marker;

va_start( marker, first ); /* Initialize variable arguments. */
while( i != -1 )
{
sum += i;
count++;
i = va_arg( marker, int);
}
va_end( marker ); /* Reset variable arguments. */
return( sum ? (sum / count) : 0 );
}

Here is one simple implementation in VC:

#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t))
)
#define va_end(ap) ( ap = (va_list)0 )

Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.