简体   繁体   English

如何将“虚拟”参数传递给 C 中的 function?

[英]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:我有这个 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.这个 function(我不能编辑也不想编辑,因为在其他情况下我需要len )创建一个文件并相应地修改len参数。 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?在某些情况下我根本不需要len ,因此我的问题是:有没有一种方法可以传递硬编码的虚拟参数而无需为此目的声明虚拟变量?

Elements of compound literals are modifyable, so you can pass its address if the function require valid addresses.复合文字的元素是可修改的,因此如果 function 需要有效地址,您可以传递其地址。

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

Note that most arrays in expressions are converted to an address of their first elements.请注意,表达式中的大多数 arrays 都被转换为其第一个元素的地址。

More information: c99 - Why are compound literals in C modifiable - Stack Overflow更多信息: c99 - 为什么 C 中的复合文字可修改 - 堆栈内存溢出

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如果您不打算使用len ,并且foo()准备好处理通过len传递的空值,您可以调用 function

foo (validparams, NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM