Join in the conversation!
Pick your username
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
Reply to Topic
AuthorMessage
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
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), United Ki
Join Date: Sep 2008

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

On March 15, 2010
New Post! Jul 11, 2009 @ 10:45:16#4
Falsifying
Google is watching.


Debater+
6107 points


Send PM


19/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 January 11, 2010
New Post! Jul 11, 2009 @ 10:45:28#5
sugarflyguy
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), United Ki
Join Date: Sep 2008

from line 19 { hasn't been closed

line 12 probably needs an int argument

On March 15, 2010
New Post! Jul 11, 2009 @ 10:47:02#6
sugarflyguy
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), United Ki
Join Date: Sep 2008

from line 19 it is

{

{

}

{

}

} think maybe that is not correct in c

On March 15, 2010
New Post! Jul 11, 2009 @ 10:47:12#7
Falsifying
Google is watching.


Debater+
6107 points


Send PM


19/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 January 11, 2010
New Post! Jul 11, 2009 @ 10:47:45#8
sugarflyguy
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), United Ki
Join Date: Sep 2008

nah, im useless hee

On March 15, 2010
New Post! Jul 11, 2009 @ 10:49:34#9
Falsifying
Google is watching.


Debater+
6107 points


Send PM


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

sugarflyguy said:

nah, im useless hee



On January 11, 2010
New Post! Jul 11, 2009 @ 10:53:19#10
sugarflyguy
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), United Ki
Join Date: Sep 2008

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

On March 15, 2010
New Post! Jul 11, 2009 @ 11:08:58#11
sugarflyguy
Pro-Pho-tographer


Monk
30376 points

Deleted

Send PM


40/M/Leaving TFS today:), 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?
On March 15, 2010
Amazon.com goodies - Books - Movies - Electronics
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 >>

TFS Time: Sun 21 Mar 2010 09:12 am CDT
Copyright © 2004-2010 Funky Llama Productions, LLC   |   Home   |   Contact   |   Privacy Policy   |   Terms of Service   |   Top of Page
The Forum Site - Your premier source for everything
Hate ads?