How can I write (if it's possible at all...) a function which takes an unknown number of parameters in C99 (the return type is constant)?
Yes you can do it in C using what are referred to as Variadic Functions.
The standard printf() and scanf() functions do this, for example.
Put the ellipsis (three dots) as the last parameter where you want the 'variable number of parameters to be.
To access the parameters include the <stdarg.h> header:
#include <stdarg.h>
And then you have a special type va_list which gives you the list of arguments passed, and you can use the va_start, va_arg and va_end macros to iterate through the list of arguments.
For example:
#include <stdarg.h>
int myfunc(int count, ...)
{
va_list list;
int j = 0;
va_start(list, count);
for(j=0; j<count; j++)
{
printf("%d", va_arg(list, int));
}
va_end(list);
return count;
}
Example call:
myfunc(4, -9, 12, 43, 217);
A full example can be found at Wikipedia.
The count parameter in the example tells the called function how many arguments are passed. The printf() and scanf() find that out via the format string, but a simple count argument can do it too. Sometimes, code uses a sentinel value, such as a negative integer or a null pointer (see
execl()
for example).
Question
Is there a way to pass many arguments to MyPrint() below using some kind of array containing a list of pointers to strings that va_start() understands before calling vsnprintf()?
Example of a format string specifier. It would be nice to create an array of the corresponding values and pass that to MyPrint() rather than individually passing each argument. I don't know if it's possible for va_start() to understand it. :(
"[0x%llX][%u] %s --- A=%llu (0x%llX) B=%llu (0x%llX) C=%llu (0x%llX) X=%llu (0x%llX) Y=%llu (0x%llX) Z=%llu (0x%llX)"
Details
MyPrint() calls vsnprintf() which prints a formatted list of arguments to a character array. The declaration for vsnprintf() is shown below:
int vsnprintf(char *arr, size_t len, const wchar_t *format, va_list args);
Parameters
arr: Pointer to the character array where output is to be printed
len: Maximum number of characters that can be written to the array
format: Format in which the output will be printed
args: Pointer to the list of arguments to be printed
Demo
#include <stdio.h>
#include <stdarg.h>
int MyPrint(char* buffer, int bufferSize, const char *format, ...)
{
int len = 0;
va_list arguments;
va_start(arguments, format);
len = vsnprintf(buffer, bufferSize, format, arguments);
va_end(arguments);
return len;
}
int main()
{
char buffer[256];
MyPrint(buffer, 256, "%s %s","Hello","World");
printf("%s",buffer);
return 0;
}
Is there a way to pass many arguments to MyPrint() below using some kind of array containing a list of pointers to strings that va_start() understands before calling vsnprintf()?
The only defined ways to initialize a va_list, such as vsnprintf() requires as a parameter, are
via the va_start() macro, operating in the context of a variadic function to form a va_list from the function's variadic arguments, and
via the va_copy() macro, to make a copy of another va_list.
There is no mechanism in standard C to form a va_list from the elements of an array, except by passing them all, individually, to a variadic function.
Variadic functions are about coding flexibility, not data flexibility. If you want a function that handles arrays of data, then write a (non-variadic) one that does so.
Whenever you consider writing your own varargs function, smack yourself in the head and repeat the mantra: "varargs is not the answer". Only if you still have varargs in your head after a few iterations of that should you should consider actually investigating that option.
How can I write (if it's possible at all...) a function which takes an unknown number of parameters in C99 (the return type is constant)?
Yes you can do it in C using what are referred to as Variadic Functions.
The standard printf() and scanf() functions do this, for example.
Put the ellipsis (three dots) as the last parameter where you want the 'variable number of parameters to be.
To access the parameters include the <stdarg.h> header:
#include <stdarg.h>
And then you have a special type va_list which gives you the list of arguments passed, and you can use the va_start, va_arg and va_end macros to iterate through the list of arguments.
For example:
#include <stdarg.h>
int myfunc(int count, ...)
{
va_list list;
int j = 0;
va_start(list, count);
for(j=0; j<count; j++)
{
printf("%d", va_arg(list, int));
}
va_end(list);
return count;
}
Example call:
myfunc(4, -9, 12, 43, 217);
A full example can be found at Wikipedia.
The count parameter in the example tells the called function how many arguments are passed. The printf() and scanf() find that out via the format string, but a simple count argument can do it too. Sometimes, code uses a sentinel value, such as a negative integer or a null pointer (see
execl()
for example).
How can I write (if it's possible at all...) a function which takes an unknown number of parameters in C99 (the return type is constant)?
Yes you can do it in C using what are referred to as Variadic Functions.
The standard printf() and scanf() functions do this, for example.
Put the ellipsis (three dots) as the last parameter where you want the 'variable number of parameters to be.
To access the parameters include the <stdarg.h> header:
#include <stdarg.h>
And then you have a special type va_list which gives you the list of arguments passed, and you can use the va_start, va_arg and va_end macros to iterate through the list of arguments.
For example:
#include <stdarg.h>
int myfunc(int count, ...)
{
va_list list;
int j = 0;
va_start(list, count);
for(j=0; j<count; j++)
{
printf("%d", va_arg(list, int));
}
va_end(list);
return count;
}
Example call:
myfunc(4, -9, 12, 43, 217);
A full example can be found at Wikipedia.
The count parameter in the example tells the called function how many arguments are passed. The printf() and scanf() find that out via the format string, but a simple count argument can do it too. Sometimes, code uses a sentinel value, such as a negative integer or a null pointer (see
execl()
for example).
Consider the following test case:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
void test(char **outa, char **outb, const char* fstra, const char* fstrb, ...) {
va_list ap;
va_start(ap, fstrb);
vasprintf(&outa, fstra, ap);
vasprintf(&outb, fstrb, ap);
va_end(ap);
}
int main(void) {
char *a, *b;
test(&a, &b, "%s", " %s\n", "foo", "bar");
/* ... */
}
The intent here is that the test() function takes two format strings and a list of parameters for both of them. The first format string is supposed to 'eat' as many arguments it needs, and the remaining ones are supposed to be used for the second format string.
So, the expected result here would be foo & bar and that's what I get with glibc. But AFAICS the machine running codepad (guess some *BSD it is), gives foo & foo and my guess is that it uses va_copy() on the argument list.
I guess I'm hitting an undefined (and ugly) behavior here; so the question is: is there a way to achieve double-format-string printf() without reimplementing it from scratch? And is there a nice way to check that behavior using autoconf without using AC_RUN_IFELSE()?
I guess some quick method of scanning format-string for the number of arguments to be consumed could work here as well (+va_copy()).
When you call one of the v*printf functions, this uses va_arg which means the value of ap is indeterminate on return.
The relevant bit lies in section 7.19.6.8 The vfprintf function in C99, which references the footnote:
As the functions vfprintf, vfscanf, vprintf, vscanf, vsnprintf, vsprintf, and vsscanf invoke theva_argmacro, the value ofargafter the return is indeterminate.
This has survived to the latest draft of C1x I have as well, so I suspect it's not going to change quickly.
There is no portable way to do what you're attempting using the higher-level v*printf functions although you could resort to using the lower level stuff.
The standard is very clear in that a called function using va_arg on a va_list variable renders it indeterminate in the caller. From C99 7.15 Variable Arguments <stdarg.h>:
The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap.
However, the value of ap when using va_arg on it within a single function is determinate (otherwise the whole variable arguments processing would fall apart). So you could write a single function which processed both format strings in turn, with these lower-level functions.
With the higher level stuff (as per the footnote), you are required to va_end/va_start to put the ap variable back in a determinate state and this will unfortunately reset to the start of the parameter list.
I'm not sure how much of a simplification your provided example code is but, if that's close to reality, you can acheive the same result by just combining the two format strings beforehand and using that to pass to vprintf, something like:
void test(const char* fstra, const char* fstrb, ...) {
char big_honkin_buff[1024]; // Example, don't really do this.
va_list ap;
strcpy (big_honkin_buff, fstra);
strcat (big_honkin_buff, fstrb);
va_start(ap, big_honkin_buff);
vprintf(big_honkin_buff, ap);
va_end(ap);
}
As the other answer already states, passing ap to a v*() function leaves ap in an undetermined state. So, the solution is to not depend on this state. I suggest an alternative workaround.
First, initialize ap as normal. Then determine the length of the first formatted string using vsnprintf(NULL, 0, fstra, ap). Concatenate the format strings, reinitialize ap, and split the output using the predetermined length of the first formatted string.
It should look something like the following:
void test(const char* fstra, const char* fstrb, ...) {
char *format, *buf;
char *a, *b;
int a_len, buf_len;
va_list ap;
va_start(ap, fstrb);
a_len = vsnprintf(NULL, 0, fstra, ap);
va_end(ap);
asprintf(&format, "%s%s", fstra, fstrb);
va_start(ap, fstrb);
buf_len = vasprintf(&buf, format, ap);
va_end(ap);
free(format);
a = malloc(a_len + 1);
memcpy(a, buf, a_len);
a[a_len] = '\0';
b = malloc(buf_len - a_len + 1);
memcpy(b, buf + a_len, buf_len - a_len);
b[buf_len - a_len] = '\0';
free(buf);
}
As also discussed in the other answer, this approach does not separate positional printf-style placeholders ("%1$s. I repeat, %1$s."). So the documentation for the interface should clearly state that both format strings share the same positional placeholder namespace—and that if one of the format strings uses positional placeholders then both must.
To complete the other answers, which are correct, a word about what happens in common implementations.
In 32bit Linux (and I think Windows too), passing the same ap to two functions actually works.
This is because the va_list is just a pointer to the place on the stack where the parameters are. v*rintf functions get it, but don't change it (they can't, it's passed by value).
In 64bit Linux (don't know about Windows), it doesn't work.
va_list is a struct, and v*printf gets a pointer to it (because actually it's an array of size 1 of structs). When arguments are consumed, the struct is modified. So another call to v*printf will get the parameters not from the start, but after the last one consumed.
Of course, this doesn't mean you should use a va_list twice in 32bit Linux. It's undefined behavior, which happens to work in some implementations. Don't rely on it.