PDA

Click to See Complete Forum and Search --> : va_start, va_arg, and va_end macros


amac
Aug 21st, 2001, 02:49 PM
I was wondering if someone that has used these and gotten them to work would help me out...

I found an example which works... but when I try to do it... it doesnt... I almost type exactly the same thing in the example and still nothing...



#include <stdio.h>
#include <stdarg.h>

int count ( int first, ... );

void main (void)
{
printf ("%d", count ( 1, 2, 3, 4, 5, 6 ) );
}

int count ( int first, ... )
{

int count = 1, i = 0;
va_list start;

va_start ( start, first );

while (1)
{

i = va_arg (start, int);

if (i != -1) count++;
else break;

}

va_end ( start );

return count;

}



This is how the example does it... by lookin at it... this should work... but doesnt.

Can anyone help?

CornedBee
Aug 23rd, 2001, 10:24 AM
you have no indication how many parameters are passed.
The (i != -1) can be used in the documentation example because every argument list has -1 as the last argument.
printf knows how many args to expect because of the formatstring
your function simply continues to retrieve "arguments" until it eventually reaches locked memory, causing an access violation.
if you call it like this: count(1, 2, 3, 4, 5, 6, 7, 8, 9, -1) it will work and return 9

all the buzzt
CornedBee