Is there an alternative method to determine no. of arguments passed to function that accepts a variable argument list ?
i read va_end is a macro that set the pointer declared by va_list to NULL after no more item is left to be traveresed. I tried this, but i get a runtime crash
Code:
// Variable No. of Arguments Passed To Function
#include<stdio.h>
#include<stdarg.h>
int findmax(int n,...);
main(void)
{
int max;
max = findmax(3,50,66,12);
printf("Max:%d",max);
max = findmax(5,10,12,5,23,100);
printf("\n\nMax:%d",max);
getchar();
}
int findmax(int num,...)
{
int max,n,k=1;
va_list ptr; // declaring pointer of type va_list
va_start(ptr,num); // initializing pointer to beginning of variable argument list
max = va_arg(ptr,int); // retrieve value, and increment the pointer
while(ptr!=NULL)
{
n = va_arg(ptr,int);
if(n>max)
max = n;
k++;
}
return(max);
}
i have just change while(k<=num) in my code to find for an alternative solution