[英]Return with no value
If I have an int function, with a return;
如果我有一个int函数,则
return;
int f(int val, int *hasResult) {
if (val) {
*hasResult = 0;
return;
}
*hasResult = 1;
return 2;
}
and use it as 并用作
int hasResult;
int unwrapped = f(val, &hasResult);
if (hasResult) {
printf("%d", unwrapped);
}
Is this valid C89? 这是有效的C89吗? I know it's not valid C99+, but can I do this, exactly as it is, in C89?
我知道这不是有效的C99 +,但是我可以在C89中做到这一点吗? If not, how would I have to do this?
如果没有,我该怎么做?
( f
doesn't have side-effects) (
f
没有副作用)
In C89 draft paragraph 3.6.6.4 we find: 在C89草案第3.6.6.4段中,我们发现:
If a return statement without an expression is executed, and the value of the function call is used by the caller, the behavior is undefined .
如果执行了不带表达式的return语句,并且调用者使用了函数调用的值,则该行为是undefined 。 Reaching the } that terminates a function is equivalent to executing a return statement without an expression.
到达终止函数的}等效于执行不带表达式的return语句。
In short, to be avoided, whether "legal"or not. 简而言之,无论是否“合法”,都应避免。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.