C 警告:声明无效

发布于 2024-10-26 14:12:03 字数 261 浏览 5 评论 0原文

当尝试使用以下内容编译我的程序时:

gcc -pedantic -Wall -ansi 

我收到警告:警告:语句无效

参考这一行:

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

任何人都可以帮我解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水染的天色ゝ 2024-11-02 14:12:03

currentDirection; 不执行任何操作。

将您的行替换为

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

Or,以防您忘记初始化变量:

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

currentDirection; does nothing.

Replace your line with

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

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

for(currentDirection = 0; currentDirection <= endDirection; currentDirection++)
背叛残局 2024-11-02 14:12:03
for(currentDirection; currentDirection <= endDirection; currentDirection++)
 // ^^^^^^^^^^^^^^^ Its saying about the above statement.

第一条语句应该有一个赋值,但在本例中没有发生这种情况,这也是警告的原因。确保为 currentDirection 分配了一个有效值,否则它可能会产生垃圾,并可能在以后导致问题。

这类似于所说的——

 int i = 10 ;
 i ;   // This statement is valid but has no effect.
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 currentDirection is assigned to a valid value or it might have garbage and might later cause issues.

It is similar to when said -

 int i = 10 ;
 i ;   // This statement is valid but has no effect.
爱情眠于流年 2024-11-02 14:12:03

根据我的经验,当您执行以下操作时,就会出现此问题:

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

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

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

要么

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

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:

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

Or

int x;
for(x = 0; x < num; x++){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文