Variable number of parameters in a function call?

F

Felix Kater

Hi,

is it possible to define a function in a way that when calling it I can
insert as many arguments as I want? (I would neither like to define a
function with 50 default arguments nor prepare a pointer array each time
before calling the function and pass the array, though.)

I imagine function calls like this:

f(a,b,c);
f(d,e,f,g,h);
f(a,
f(b,
c,
f(g,h,i),
e,
g,
i,
c);

Felix
 
M

Malcolm

Felix Kater said:
is it possible to define a function in a way that when calling it I can
insert as many arguments as I want?

void foo(int N, ...)

Look up stdarg in your documentation to see how to access the arguments

You must have some way of telling the function how many arguments you have
passed, which means the first argument must be of fixed type.
 
M

Merrill & Michele

"Malcolm wrote:
void foo(int N, ...)

Look up stdarg in your documentation to see how to access the arguments

You must have some way of telling the function how many arguments you have
passed, which means the first argument must be of fixed type.

Is it possible that you're asking this question not knowing the nuts and
bolts of recursion? MPJ
 
A

Artie Gold

Merrill said:
Is it possible that you're asking this question not knowing the nuts and
bolts of recursion? MPJ
I fail to see the relevance of your comment.

?

--ag
 
M

Merrill & Michele

"Artie Gold" wrote : I fail to see the relevance of your comment. ?

The sentence you appended, although followed by a question mark, was a
comment. The sentence I appended, previous to yours, was a question, as
properly indicated by the question mark. If Felix the Cat is just a guy who
knows animated soft porn as opposed to set theory, then this thread can
gladly continue without my attention. MPJ
 
M

Malcolm

Artie Gold said:
I fail to see the relevance of your comment.
The OP is calling f() with the return value of f() as an argument. So it is
possible that what he is really after is recursion, rather than variadic
functions.
 
F

Felix Kater

Merrill & Michele:
If Felix the Cat is just a guy who
knows animated soft porn as opposed to set theory, then this thread
can gladly continue without my attention.

Hm. Seems to be angry.
 
F

Felix Kater

Merrill & Michele:
Is it possible that you're asking this question not knowing the nuts
and bolts of recursion?

No, I am not asking for recursion.

Felix
 
K

Kenny McCormack

Merrill & Michele:


No, I am not asking for recursion.

Felix

Then why do you give an example of f(...,f(...),...) ?

It is certainly possible, of course, that this has nothing to do with the
stated question (how to use the varargs capability of stdarg.h), but it
acts as a potential decoy on people's attention.
 
F

Felix Kater

Kenny McCormack:
it
acts as a potential decoy on people's attention.

I took this example to offer you the possibility to look for solutions
suitable to my problem. With this I wanted to be sure that the provided
solution (which I was looking for) could handle nested situations as
well.

Felix
 
J

John Bode

Felix Kater said:
Hi,

is it possible to define a function in a way that when calling it I can
insert as many arguments as I want? (I would neither like to define a
function with 50 default arguments nor prepare a pointer array each time
before calling the function and pass the array, though.)

I imagine function calls like this:

f(a,b,c);
f(d,e,f,g,h);
f(a,
f(b,
c,
f(g,h,i),
e,
g,
i,
c);

Felix


Read up on stdarg.h and variadic functions in your favorite C
reference manual. Note that in Standard C, at least one argument
*must* be of a fixed type. You must also figure out a way to indicate
the number and types of arguments being passed.

Quick, dirty, *UNTESTED* example:

#include <stdio.h>
#include <stdarg.h>

typedef enum {
EMPTY, // No additional arguments
RECTYPE_1, // 1 int, 2 double
RECTYPE_2 // 2 char *, 2 int
} ArgsType;

int func(ArgsType t, ...)
{
va_list *ap;
char *a1, *a2;
int i1, i2;
double d1, d2;

int r = 0;

va_start(ap, t);

switch(t)
{
case EMPTY:
/* do something interesting */
break;

case RECTYPE_1:
i1 = va_arg(ap, int);
d1 = va_arg(ap, double);
d2 = va_arg(ap, dounble);
/* do something interesting with i1, d1, d2 */
break;

case RECTYPE_2:
a1 = va_arg(ap, char *);
a2 = va_arg(ap, char *);
i1 = va_arg(ap, int);
i2 = va_arg(ap, int);
/* do something interesting with a1, a2, i1, and i2 */
break;
}

return r; /* r set in interesting bits */
}

int main (void)
{
func(EMPTY);
func(RECTYPE_1, 2, 4.0, 8.0);
func(RECTYPE_2, "foo", "bar", 5, func(RECTYPE_1, 3, 1.0, 0.0));
return 0;
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top