0

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?

j0s3
  • 13
  • 1
  • 2
  • 3
    If `foo` allows it, you might just pass NULL. If `foo` does not allow it, you will need a valid address. – William Pursell Sep 22 '20 at 12:39
  • 1
    Why exactly can't you declare a dummy parameter/parameters? A variable named "dummy" is a perfect example of self-documenting code. It is common that function are written to accept NULL for optional parameters. – Lundin Sep 22 '20 at 12:51
  • Are you allowed to modify the function _implementation_ (not the function _prototype_)? – Jabberwocky Sep 22 '20 at 12:52

2 Answers2

1

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

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

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);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261