0

Consider below function:

void foo(int n,...);

I need to call this function, "optionally" passing variable arguments.

Is it possible?

Something like this:

foo(10, (bIsJobDone ? "OK" : xxx) );

I am not able to figure out what to put in place of xxx (xxx should convert to nothing) and also how to avoid "," after 10, if I don't have to pass any variable arg to foo?

Note: i can't change signature of "foo".

3
  • What do you mean with xxx, another type than a string, or nothing at all? Commented Sep 3, 2018 at 6:22
  • @Lundin xxx should convert to nothing. Commented Sep 3, 2018 at 6:28
  • That case is useless; how should the called function determine if there is a second argument, if the first one is the same in both calls? For varargs function, there has to be some information in the fixed arguments that tell the called function how many variable arguments to expect (or you have to use a sentinel, i.e. there's always at least one variable argument which might be the sentinel). Inside foo() you can't differentiate foo(10) from foo(10, "OK"). Commented Sep 3, 2018 at 8:50

3 Answers 3

2

As long as you got the called function correctly prototyped and defined (not easy...),
calling it with one or two parameter, depending on a condition is easy:

if(bIsJobDone)
{    foo(10, "OK");
} else
{    foo(10);
}

The alternative, to squeeze it into a ternary operator, is possible, but considered unreadable by many (just a matter of opinion of course) and not needed for the void-returning prototype you have shown in your question.

Sign up to request clarification or add additional context in comments.

Comments

0

Call it separately:

bIsJobDone ? foo(10, "OK") : foo(10);

Comments

0

If a function has been written for variable number of arguments (e.g. printf), then it is possible to pass a variable number of arguments to a function.

These work with either of two patterns

The Map Pattern

Like printf, an early non optional parameter describes the remaining parameters which have been added. These are then plucked from the stack appropriately.

The terminating pattern

A function can be written to sum a set of parameters, which have some mechanism for detecting the terminator.

int sum( int first_arg, ... );

Where a special token is added to the function to call correctly.

If a function has been written to only accept a fixed number of arguments, you have to pass that number of arguments.

This is because the calling code, and the called code have to agree on how much stack is used. Any difference in this contract, will result in undefined behavior, which can mean a crash, or you program may be exploitable by a malicious actor.

Given that the function has a set number of parameters, you need to supply all of these.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.