C Programming - Variable Number of Arguments - Discussion

Discussion Forum : Variable Number of Arguments - Point Out Errors (Q.No. 4)
4.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>

int main()
{
    void display(char *s, int num1, int num2, ...);
    display("Hello", 4, 2, 12.5, 13.5, 14.5, 44.0);
    return 0;
}
void display(char *s, int num1, int num2, ...)
{
    double c;
    char s;
    va_list ptr;
    va_start(ptr, s);
    c = va_arg(ptr, double);
    printf("%f", c);
}
Error: invalid arguments in function display()
Error: too many parameters
Error: in va_start(ptr, s);
No error
Answer: Option
Explanation:
We should have use va_start(ptr, num2);
Discussion:
11 comments Page 1 of 2.

Subra said:   9 years ago
Yes, we are getting redeclared error "s".

Vinay said:   1 decade ago
Error: "s" is re declared.

Piyush Ashtikar said:   1 decade ago
void display (char*s, int num1, int num2..).

As num2 is last argument of display just before ellipses(.), it should be second argument of va_start().

HantsColin said:   1 decade ago
Surely there is an error with display() as the variable 's' is defined in both the parameter list and as a local variable.

void display(char *s, int num1, int num2, ...)
{
double c;
char s;

Yogendra said:   1 decade ago
Because last statement is,

c = va_arg (ptr, double) ;

So need for execution that must require (ptr, mun3) for float value.

Than just befor statement.

va_start (ptr, s) ; start from va_start (ptr, num2) ; must it.

BHASKAR PATHAK said:   1 decade ago
@Krunal.

Because va_start is a macro which accepts two arguments, va_list and the name of the variable that directly precedes the ellipsis (...).

Krunal said:   1 decade ago
Why not va_start (ptr, num1) ; ?

Priyanka said:   1 decade ago
va_start (ptr, s) ; is a calling function, but here their is no definition i.e. no called function.

For example in this function you have void display (char *s, int num1, int num2, ... ) ; in the main i.e., the calling function.

What is written in the function after the main is the function definition.

Bhakar said:   1 decade ago
We should have use va_start(ptr, num2); cause we write always use last fixed parameter ..in this fixed parameter is num not char

Swetha said:   1 decade ago
Please explain the program.


Post your comments here:

Your comments will be displayed after verification.