Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
ProgrammersTalk
Junior Poster in Training
84 posts since Jun 2007
Reputation Points: 21
Solved Threads: 7
Code not tested, but see no reason why it shouldn't work. :)
Cheers.
Agree it should work but not the way the OP intended because it does not allow for variable number of arguments.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
1) What's the OP?
2) didn't want to over complicate things for the guy, as he seems to be just starting out in C, giving him a basic understanding of parameter passing would be more beneficial without talking to him about 'VA'.
That's my opinion anyway.
OP = Original Poster. I thought OP was a pretty common term, but maybe I was wrong. That's what I get for using an anronym.
He asked for variable arguments -- the code you posted did nothing to answer that question. I don't know his experience and neither does anyone else. He must have at least some programming knowledge in order to even ask the question.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Haha, i thought he ment passing variable arguments.. as you can pass an array as a parameter (using pointers of course), so i thought he ment how do you pass a variable to a function.
Oh well :)
Oh, now I understand the reason for your post. :)Dear friend , I am asking about a function like printf(). as it takes the variable argument.How can we define a function like this?
Most functions that take a variable number of arguments are declared with at least one real argument followed by three periods, like the code posted by Narue and TkTkorrovi.
printf() does not use the periods because of theformat parameter which indicates the number of arguments and their data types.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>Most functions that take a variable number of arguments are declared
>with at least one real argument followed by three periods
All of them, actually. :) va_start requires the last non-variadic parameter as its second argument, so at least one non-variadic parameter is required as well as the ellipsis to specify further variadic parameters.
>printf() does not use the periods because of the format parameter
>which indicates the number of arguments and their data types.
Since when? This is the prototype for printf, and I see an ellipsis:
int printf ( const char *format, ... );
The format string is for evaluating the variadic parameters, it's not a replacement for the required syntax.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Narue: thanks for the correction :icon_redface:
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343