variable arguments in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: variable arguments in C

 
0
  #11
Jul 16th, 2007
Originally Posted by dr4g View Post
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.


Originally Posted by hitesh_mathpal View 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 the format parameter which indicates the number of arguments and their data types.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: variable arguments in C

 
0
  #12
Jul 16th, 2007
>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:
  1. int printf ( const char *format, ... );
The format string is for evaluating the variadic parameters, it's not a replacement for the required syntax.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: variable arguments in C

 
0
  #13
Jul 16th, 2007
Narue: thanks for the correction
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: variable arguments in C

 
0
  #14
Jul 16th, 2007
It's tested now
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3.  
  4. void stringstofile (FILE *fp, ...)
  5. {
  6. char *p;
  7. va_list ap;
  8.  
  9. va_start (ap, fp);
  10. while ((p = va_arg (ap, char *)))
  11. fprintf (fp, "%s\n", p);
  12. va_end (ap);
  13. }
  14.  
  15. int main ()
  16. {
  17. stringstofile (stdout, "one",
  18. "two", "three", NULL);
  19. return 0;
  20. }
and the output is
  1. one
  2. two
  3. three
Last edited by TkTkorrovi; Jul 16th, 2007 at 12:43 pm. Reason: gcc
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC