[英]Is this code segment legal in C?
function wait() is declared in another function. 函数wait()在另一个函数中声明。 Is it legal? 这合法吗?
void panic(const int reason, const char *strg)
{
int ErrNo;
struct machine_attributes mach;
int ret, docstat, cnt;
pid_t pid, wait(int *), setsid(void);
......
}
Thank you! 谢谢!
Yes, so long as this declaration matches the actual definition of the function. 是的,只要此声明与函数的实际定义相匹配。
pid_t pid, wait(int *), setsid(void);
This declares three entities: a pid_t
named pid
, a function (taking int*
and returning pid_t
) named wait
and a function (taking no parameters and returning pid_t
) named setsid
. 该声明了三个实体: pid_t
名为pid
,函数(取int*
和返回pid_t
)命名的wait
和功能(不采取任何参数和返回pid_t
)命名setsid
。
The declaration of pid
is also a definition. pid
的声明也是一个定义。
Yes it is legal C, and it could be useful in rare cases, for instance if you have a plain C (non-POSIX-oriented) source file that uses wait
with static
linkage for a function of its own, and suddenly realize you need to call the POSIX wait
from a function in that file. 是的,它是合法的C,在极少数情况下它可能是有用的,例如,如果你有一个普通的C(非POSIX导向)源文件,它使用static
链接wait
它自己的功能,突然意识到你需要从该文件中的函数调用POSIX wait
。 By scoping the declaration in the function that calls it, you avoid conflicting with the file-scope static
definition of wait
. 通过在调用它的函数中确定声明范围,可以避免与wait
的文件范围static
定义冲突。
Note that pid_t
may be obtained from other headers that do not declare wait
(or any functions), but in other cases you might not be able to use a trick like this due to missing types. 请注意, pid_t
可以从其他未声明wait
(或任何函数)的标头中获取,但在其他情况下,由于缺少类型,您可能无法使用这样的技巧。
And yes, some may call this a horrible hack/abuse of the language. 是的,有些人可能会称这是一种可怕的黑客/滥用语言。 :-) :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.