By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sun, 07 Dec 2003 01:38:51 -0700, in comp.lang.c , Jeff Rodriguez
<ne********@gurugeek.EXAMPLENOSPAM.com> wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.