Post in Forums
Create a Profile
Upload Pictures
Make Polls
Keep a Journal
Meet Friends
Have Fun
It's FREE!
Sign Up!
Avoid the cold & the crowds - Buy your Christmas gifts at Amazon.com
Forum Index > Computers & Internet > Programming
>> About variable arguments in c
Reply to Topic
Search Topic
Subscription
Author Message
New Post! Jul 11, 2009 @ 10:16:07#1
sakthitg

Recruit
12 points


Send PM


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.

On July 13, 2009
New Post! Jul 11, 2009 @ 10:42:16#3
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


39/M/Somewhere in the UK, United Ki
Join Date: Sep 2008

Would it be possible to number in the lines, bit confusing like this




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
On November 22, 2009
New Post! Jul 11, 2009 @ 10:45:16#4
Falsifying
Google is watching.


Debater+
6075 points


Send PM


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)
On November 21, 2009
New Post! Jul 11, 2009 @ 10:45:28#5
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


39/M/Somewhere in the UK, United Ki
Join Date: Sep 2008

from line 19 { hasn't been closed

line 12 probably needs an int argument




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
On November 22, 2009
New Post! Jul 11, 2009 @ 10:47:02#6
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


39/M/Somewhere in the UK, United Ki
Join Date: Sep 2008

from line 19 it is

{

{

}

{

}

} think maybe that is not correct 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
On November 22, 2009
New Post! Jul 11, 2009 @ 10:47:12#7
Falsifying
Google is watching.


Debater+
6075 points


Send PM


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.
On November 21, 2009
New Post! Jul 11, 2009 @ 10:47:45#8
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


39/M/Somewhere in the UK, United Ki
Join Date: Sep 2008

nah, im useless hee




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
On November 22, 2009
New Post! Jul 11, 2009 @ 10:49:34#9
Falsifying
Google is watching.


Debater+
6075 points


Send PM


18/M/Austin, Texas
Join Date: Dec 2008

sugarflyguy said:

nah, im useless hee



On November 21, 2009
New Post! Jul 11, 2009 @ 10:53:19#10
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


39/M/Somewhere in the UK, United Ki
Join Date: Sep 2008

Oh dear, doing my typing here, studying all that code




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
On November 22, 2009
New Post! Jul 11, 2009 @ 11:08:58#11
sugarflyguy
Aye-curumba


Ogler+
24916 points


Send PM


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
On November 22, 2009
New Post! Jul 13, 2009 @ 05:20:57#12
sakthitg

Recruit
12 points


Send PM


22/NA/Madurai, India
Join Date: Jul 2009

arg = va_arg (ap, int );


That is the syntax for the macro va_arg.

int is a type caste input to va_arg.

On July 13, 2009
Reply to Topic<< Previous Topic | Next Topic >>

Top

TFS Time: Sun 22 Nov 2009 12:17 am CST
Copyright © 2004-2009 Funky Llama Productions, LLC   |   Home   |   Contact   |   Privacy Policy   |   Terms of service

The Forum Site - Your premier source for everything LOGIN REGISTER