some inner thoughts on va_start()

S

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.


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 );
}
 
A

Alf P. Steinbach

* 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.
 
R

Ron Natalie

Sekhar said:
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).
 
J

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
 
Y

Yong Hu

Sekhar said:
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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top