得到“无效使用 void 表达式”背后的原因是什么? C 中的错误?
我这里有这段 C 代码:-
#include<stdio.h>
void message();
int main()
{
message(message());
return 0;
}
void message()
{
printf("hello\n");
}
编译器抛出一条错误消息,读取语句块“message(message))”的“无效使用 void 表达式”。
我预计输出是 printf() 语句的两倍,根据我的说法,语句 message(message());表示一旦内部函数调用执行并且控制返回到 main,之后再次执行外部调用。但是,我在这里收到错误消息“无效使用 void 表达式”错误。
我已经阅读了一些解释,但仍然无法理解。
I have this piece of C code here :-
#include<stdio.h>
void message();
int main()
{
message(message());
return 0;
}
void message()
{
printf("hello\n");
}
The compiler throws an error message reading " Invalid use of void expression" for the statement block "message(message))".
I expected the output to be two times the printf() statement as according to me, the statement message(message()); indicates that once the inner function call executes and the control returns to main and after that again the outer call executes. However I am getting the error message "Invalid use of void expression" error here.
I have read some explanations but still i'm unable to understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数消息的返回类型是 void
因此调用传递不完整类型 void 的参数的函数会引发错误。
如果要调用该函数两次,请将
or 写为一个表达式。
注意,最好像
向编译器提供函数原型一样声明该函数。
如果您想调用指定其参数作为其本身调用的函数,则应按以下方式声明和定义该函数
,并且可以像这样调用该函数
The return type of the function message is void
So calling the function passing an argument of the incomplete type void invokes an error.
If you want to call the function twice then write
or as one expression
Pay attention to that it is better to declare the function like
providing to the compiler the function prototype.
If you want to call the function specifying its argument as a call of it itself then the function should be declared and defined the following way
and the function can be called like