j0s3
j0s3

Reputation: 13

How to pass "dummy" argument to a function in C?

For the purpose of the question I don't report all the code but just the significant details. I have this function:

void foo(/* SOME OTHER ARGS */ int* len);

This function (that I can't edit and don't want edit since in other circumstances I need len) creates a file and modifies len parameter accordingly. In some occasion I don't need len at all, and hence my question is: is there a way to pass a hard-coded dummy parameter without declaring a dummy variable just for this purpose?

Upvotes: 0

Views: 1470

Answers (2)

MikeCAT
MikeCAT

Reputation: 75062

Elements of compound literals are modifyable, so you can pass its address if the function require valid addresses.

foo(/* SOME OTHER ARGS */, (int[]){0});

Note that most arrays in expressions are converted to an address of their first elements.

More information: c99 - Why are compound literals in C modifiable - Stack Overflow

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134346

If you don't intend to use len, and foo() is geared up to handle a null-value passed through len, you can call the function like

foo (validparams, NULL);

Upvotes: 0

Related Questions