ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?)
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?
The parameter last is the name of the last parameter
before the variable argument list, i.e., the last parame-
ter of which the calling function knows the type.
last is the second argument to va_start() here. fmt from my example is a string like "%d %s %p" as you suspected. Anything following the format string in my tprintf() function are variables to be used for substitution. Therefore the fmt is the last paramater with a known type.
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() 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.