C Programming - Variable Number of Arguments - Discussion
Discussion Forum : Variable Number of Arguments - Point Out Errors (Q.No. 6)
6.
Point out the error in the following program.
#include<stdio.h>
#include<stdarg.h>
void display(char *s, ...);
void show(char *t, ...);
int main()
{
display("Hello", 4, 12, 13, 14, 44);
return 0;
}
void display(char *s, ...)
{
show(s, ...);
}
void show(char *t, ...)
{
int a;
va_list ptr;
va_start(ptr, s);
a = va_arg(ptr, int);
printf("%f", a);
}
Answer: Option
Explanation:
The call to show() is improper. This is not the way to pass variable argument list to a function.
Discussion:
11 comments Page 1 of 2.
Zdd said:
7 years ago
How about option D?
Chetan said:
8 years ago
Please, explain the correct syntax.
Christobal said:
9 years ago
The error I got was in function display ().
Error C2059: syntax error : '. '.
Therefore I think the correct solution is incorrect function display. This is more true than incorrect function show. The compiler will first encounter the issue in function display() since display is first.
Error C2059: syntax error : '. '.
Therefore I think the correct solution is incorrect function display. This is more true than incorrect function show. The compiler will first encounter the issue in function display() since display is first.
Sanghamitra Das said:
1 decade ago
Please clearly explain how display() and show() work in this program?
Dharmveer singh said:
1 decade ago
void is also a return type that return void.
Then why it gets an error?
Then why it gets an error?
D Souza said:
1 decade ago
I'm new to this concept. But from what I have understood I think the display function should have been written like this,
display(char *s, ...)
{
int a,b,c,d,e;
va_list ptr;
va_start(ptr, char *);
a=va_arg(ptr, int);
//similarly code for the next 5 integers with b,c,d,e
show(char *s,a,b,c,d,e);
}
NOTE: I'm still not sure about how correct this code is?
display(char *s, ...)
{
int a,b,c,d,e;
va_list ptr;
va_start(ptr, char *);
a=va_arg(ptr, int);
//similarly code for the next 5 integers with b,c,d,e
show(char *s,a,b,c,d,e);
}
NOTE: I'm still not sure about how correct this code is?
Rana said:
1 decade ago
Can anyone explain clearly how to call show() function inside the void display(char *s,...) function?
Chandra sekhar said:
1 decade ago
Here answer is correct.
To understand this program, we have to know about the symbol
ellipsis (...) this is used in functions as argument.
This makes functions concept more comfortable..... ellipsis is having three continuous dots without spaces.it is used as second or third argument.when we use this, we can call the function by pass any number of parameters.BUT ELLIPSIS CAN NOT USED AS PARAMETER.
example-1: void abc(char*,...); function abc() is able to receive addresses of any no.of character variables as arguments successfully.
example-2: void xyz(int*,char*,...): function xyz() is able to receive addresses of any no.of variables(any datatype but 1st,2nd must be integer and character).
To understand this program, we have to know about the symbol
ellipsis (...) this is used in functions as argument.
This makes functions concept more comfortable..... ellipsis is having three continuous dots without spaces.it is used as second or third argument.when we use this, we can call the function by pass any number of parameters.BUT ELLIPSIS CAN NOT USED AS PARAMETER.
example-1: void abc(char*,...); function abc() is able to receive addresses of any no.of character variables as arguments successfully.
example-2: void xyz(int*,char*,...): function xyz() is able to receive addresses of any no.of variables(any datatype but 1st,2nd must be integer and character).
Girish nischel said:
1 decade ago
Here I could not understand the line.
Void display (char*s, . ).
i.e the. In all lines! what do they mean?
Void display (char*s, . ).
i.e the. In all lines! what do they mean?
Nayak said:
1 decade ago
I need a clear description about it. Can any body suggest me how?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers