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.

Riz said:   1 decade ago
Can anyone explain?

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.

Prem said:   1 decade ago
Please kindly explain in brief.

Sunil kumar yadav said:   1 decade ago
Kindly explain in briefly?

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

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

Siva said:   1 decade ago
We need to declare the pointer *ptr as va_list type because this is variable argument function 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.

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.


Post your comments here:

Your comments will be displayed after verification.