C语言 C 警告:声明无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5417370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:08:31  来源:igfitidea点击:

C Warning: Statement with no effect

cfor-loopwarnings

提问by aytee17

When trying to compile my prgram with:

尝试使用以下命令编译我的程序时:

gcc -pedantic -Wall -ansi 

I get the warning: warning: statement with no effect

我收到警告:警告:声明无效

Referring to this line:

参考这一行:

for(currentDirection; currentDirection <= endDirection; currentDirection++)

Can anyone help me with this?

谁能帮我这个?

回答by ThiefMaster

currentDirection;does nothing.

currentDirection;什么也没做。

Replace your line with

将您的线路替换为

for(; currentDirection <= endDirection; currentDirection++)

Or, in case you just forgot to initialize the variable:

或者,如果您只是忘记初始化变量:

for(currentDirection = 0; currentDirection <= endDirection; currentDirection++)

回答by Mahesh

for(currentDirection; currentDirection <= endDirection; currentDirection++)
 // ^^^^^^^^^^^^^^^ Its saying about the above statement.

First statement should have an assignment, which is not happening in this case and is the reason for the warning. Make sure currentDirectionis assigned to a valid value or it might have garbage and might later cause issues.

第一个语句应该有一个赋值,这在这种情况下不会发生,并且是警告的原因。确保currentDirection分配给一个有效的值,否则它可能有垃圾,以后可能会导致问题。

It is similar to when said -

这类似于说 -

 int i = 10 ;
 i ;   // This statement is valid but has no effect.

回答by Cody Syring

In my experience this issue comes up when you do somthing along the lines of

根据我的经验,当您按照以下方式执行某些操作时会出现此问题

int x = 0;
for(x = 0;x < num; x++){}

When you are declaring your loop and you already initialize your variable, x you don't need to declare it a second time. So either do:

当您声明循环并且已经初始化变量 x 时,您不需要再次声明它。所以要么做:

int x = 0;
for(; x < num; x++){}

Or

或者

int x;
for(x = 0; x < num; x++){}