C Programming - Variable Number of Arguments - Discussion

Discussion Forum : Variable Number of Arguments - Point Out Errors (Q.No. 7)
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);
}
Error: too many parameters
Error: invalid access to list member
Error: ptr must be type of va_list
No error
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Nuzhat said:   1 decade ago
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...

Vivek said:   1 decade ago
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.

Sacy143 said:   1 decade ago
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.

Zeus said:   7 years ago
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.

Lotus said:   7 years ago
As the number of arguments passed to varfun is infinite which is unpredictable.

So memory space is not allocated.

Siva said:   1 decade ago
We need to declare the pointer *ptr as va_list type because this is variable argument function type.

Rujuta kelkar said:   1 decade ago
PlZ explain. Its kinda difficult to understand. We need more explanation.

Rajan said:   1 decade ago
Sir please explain the answer of this question.

Priya said:   7 years ago
Explain what is the function of va_list()?

Bond said:   1 decade ago
Is there any one who can explain it ?


Post your comments here:

Your comments will be displayed after verification.