C Programming - Variable Number of Arguments - Discussion

7. 

Point out the error in the following program.

#include<stdio.h>
#include<stdarg.h>
void varfun(int n, ...);

int main()
{
    varfun(3, 7, -11.2, 0.66);
    return 0;
}
void varfun(int n, ...)
{
    float *ptr;
    int num;
    va_start(ptr, n);
    num = va_arg(ptr, int);
    printf("%d", num);
}

[A]. Error: too many parameters
[B]. Error: invalid access to list member
[C]. Error: ptr must be type of va_list
[D]. No error

Answer: Option C

Explanation:

No answer description available for this question.

Riz said: (Sep 28, 2010)  
Can anyone explain?

Rujuta Kelkar said: (Jul 28, 2011)  
PlZ explain. Its kinda difficult to understand. We need more explanation.

Rajan said: (Aug 18, 2011)  
Sir please explain the answer of this question.

Prem said: (Aug 22, 2011)  
Please kindly explain in brief.

Sunil Kumar Yadav said: (Sep 11, 2011)  
Kindly explain in briefly?

Bond said: (Dec 1, 2011)  
Is there any one who can explain it ?

Nuzhat said: (Dec 6, 2011)  
va_list(variable list) is user defined data type...
va_list ptr;
=>argument pointer(ptr) traverse through the variable arguments passed in the function...
va_start(ptr,n);
=>points ptr to the argument, in dis case n ie 3
va_arg
=>moves teh ptr to next variable argument
num = va_arg(ptr, int);
so printf print 7...

Siva said: (Jan 18, 2013)  
We need to declare the pointer *ptr as va_list type because this is variable argument function type.

Sacy143 said: (May 22, 2015)  
va_list it hold the no. of arguments and data type and it's define in header file #include<stdarg.h>.

It should be use when you going to developed the function.

Vivek said: (Jun 11, 2015)  
Yes. Because in advance we do not know the data type. So we can not declare pointer variable to a particular data type. So we must have to declare the pointer variable of va_list type.

Ratna said: (Sep 2, 2017)  
Please explain in briefly.

Rathika said: (Mar 3, 2018)  
Please explain the program briefly.

Lotus said: (Mar 20, 2018)  
As the number of arguments passed to varfun is infinite which is unpredictable.

So memory space is not allocated.

Zeus said: (Jul 28, 2018)  
The function varfun() can take only int type parameters but we are sending float type parameters too.

So shouldn't the Error be too many parameters.

Nithya said: (Aug 18, 2018)  
Please explain the answer.

Priya said: (Jan 10, 2019)  
Explain what is the function of va_list()?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.