C Function Arguments
I would like to create a function similar to functions like printf and fprintf in C.
In stdio.h, a function such as fprintf is defined as extern int fprintf (FILE *__restrict __stream, __const char *__restrict __format, ...); I'm assuming the '...' portion references the fact that multiple variables can be passed to fprintf (if you have a const char* of "%s %d %f %s" you would then pass four additional arguments. Is this how I define a function if I want to create such functionality? And if so how do I access these additional arguments once inside the function? (is this another argc, argv scenario?) Thanks for any help, jpbarto |
|
Excellent! Thank you so much. And I'll be bookmarking that page.
Thanks, jpbarto |
Here's a function I wrote and added to my library of useful things :)
Code:
char *tprintf(char *fmt, ...) |
According to the example (and the manual) over at the GNU C library page, I got the impression that va_start's first argument was va_list and the second was an int indicating the number of the last argument. Did I get the wrong impression?
From your example I'd imagine that format might contain a string of "%s %d\n"?, so this can be passed and will initialize a va_list properly? |
Quote:
Here's an example usage of my tprintf() function just in case it helps visualize it. It works just like sprintf(), except it returns the formatted string instead of storing it in a buffer you pass to it: send_msg(player, tprintf("%d apples", num_apples)); send_msg() is of course a hypothetical function that I just came up with for the example :) To explain why it needs to know the last argument that isn't part of the substitution variable list, consider reversing the first 2 arguments of sprintf() so the protype looks like: sprintf(char *fmt, char *buffer, ...); In this case, the second argument to va_start() would be buffer, not fmt. |
I see, thanks for the clarification.
|
All times are GMT -5. The time now is 07:14 AM. |