|
Post in Forums
Create a Profile
Upload Pictures
Make Polls
|
Keep a Journal
Meet Friends
Have Fun
It's FREE!
|
|
Sign Up!
|
|
|
Forum Index > Computers & Internet > Programming | >> About variable arguments in c | | |
|
sakthitg
Recruit 12 points


22/NA/Madurai, India Join Date: Jul 2009 | Hi,
I am new to var arg concept in c. I ve tried one small code which adds 5 elements using var args.
#include <stdio.h>
#include <stdarg.h>
int main()
{
int no = 5, i = 0;
int n[25], sum = 0, ret;
printf ("Enter five numbers: \n" ;
for (i = 0; i < 5; i++)
{
scanf ("%d", &n);
}
add (no, &sum, n[0], n[1], n[2], n[3], n[4]); /* line no. 12 */
printf ("In main \n" ;
printf ("Sum of five numbers is : %d\n", sum);
return 0;
}
int add (int no, int *sum, int a, int b, ...)
{ /* line no. 19*/
va_list ap;
int arg[no], i = 2;
arg[0] = a;
arg[1] = b;
va_start (ap, b);
while(i < no)
{
arg = va_arg (ap, int );
i++;
}
va_end (ap);
for (i = 0; i < no; i++)
{
sum = sum + arg;
}
printf ("Sum of %d numbers is : %d\n", no, sum);
return 0;
}
While compiling this code, I am getting error as follows.
var-args.c:19: error: conflicting types for âaddâ
var-args.c:19: note: a parameter list with an ellipsis canât match an empty parameter name list declaration
var-args.c:12: error: previous implicit declaration of âaddâ was here
Can anybody help me with fixing this bug??
Regards,
Sakthi. | | |
|
Falsifying
Google is watching.
Debater+ 6075 points


18/M/Austin, Texas Join Date: Dec 2008 | sugarflyguy said:
Would it be possible to number in the lines, bit confusing like this
He did.
Lines 12 and 19 have errors, but his post glitched a little.
So the aadda part makes no sense. (Probably just add) | | |
|
Falsifying
Google is watching.
Debater+ 6075 points


18/M/Austin, Texas Join Date: Dec 2008 | sakthitg said:
{ /* line no. 19*/
va_list ap;
int arg[no], i = 2;
arg[0] = a;
arg[1] = b;
va_start (ap, b);
while(i < no)
{
You're right Fly.  | | |
|
sugarflyguy
Aye-curumba
Ogler+ 24942 points


39/M/Somewhere in the UK, United Ki Join Date: Sep 2008 | For line 19, specially line 27, you are using int, in the coding
Quote: arg = va_arg (ap, int ); is that allowable in c?

Now with better color management profile
1 Corinthians 13
Hello, farewell to your love and soul
Hello, farewell to your SOOOOOOUUUUULLLLLL!!!! - Peter Hook 1987 Brixton Academy | | |
Top
|