how can we pass variable arguments in any function of C?
hitesh_mathpal 0 Newbie Poster
Recommended Answers
Jump to PostI think this should work, but i didn't test it, please do it yourself.
#include <stdarg.h> void stringstofile (FILE *filehandle, ...) { va_list vargs; va_start (vargs, filehandle); while (va_arg (vargs, char *)) fputs (va_arg (vargs, char *), filehandle); va_end (vargs); }
Jump to Post>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 …
Jump to PostIt's tested now
…#include <stdio.h> #include <stdarg.h> void stringstofile (FILE *fp, ...) { char *p; va_list ap; va_start (ap, fp); while ((p = va_arg (ap, char *))) fprintf (fp, "%s\n", p); va_end (ap); } int main () { stringstofile (stdout, "one", "two", "three", NULL); return 0; }
All 13 Replies
Narue 5,707 Bad Cop Team Colleague
TkTkorrovi 69 Junior Poster
ProgrammersTalk 11 Junior Poster in Training
dr4g 25 Junior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
dr4g 25 Junior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
hitesh_mathpal 0 Newbie Poster
dr4g 25 Junior Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Narue 5,707 Bad Cop Team Colleague
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
TkTkorrovi 69 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.