Functions with variable amount of arguments i.e. printf()

J

Jeff Rodriguez

How does one go about creating and accessing the arguments of a function like this?

For example, I would like to do something along these lines:

spawn_child("/bin/ls", "-a", "-l");

Now, for testing I want to print out all the variables sent to the function. How
would I do that?
 
M

Mark McIntyre

How does one go about creating and accessing the arguments of a function like this?

Read up on va_args. va_start, va_end etc in stdarg.h.

The ISO standard has this example on page 264 which may or may not
make sense to you.
#include <stdarg.h>
#define MAXARGS 31
void f1(int n_ptrs, ...)
{
va_list ap;
char *array[MAXARGS];
int ptr_no = 0;
if (n_ptrs > MAXARGS)
n_ptrs = MAXARGS;
va_start(ap, n_ptrs);
while (ptr_no < n_ptrs)
array[ptr_no++] = va_arg(ap, char *);
va_end(ap);
f2(n_ptrs, array);
}

You might want to find an opensource version of printf to examine for
some further tips eg handling different types of parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top