读取循环变量时出错 [英] Error while reading of loop variable

查看:62
本文介绍了读取循环变量时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我正在使用此代码:

Hello!
I'm working with this code:

int counter = 0;
int dest_pos = 20;

for (int z = 0; z < 1000; z++)
{
/***2***/

   if(counter++ == dest_pos)
   {
   /***1***/
   //if reach dest position - than would set new value for dest_pos
   dest_pos = 56;
   
   }
}





在该行(标记为/ *** 1 ** * /)我试图读取z值:



At the line (marked as /***1***/) I tried to read z-value:

Memo1->Lines->Add(IntToStr(z));



我有0;



但是如果我尝试将z的值赋给标记为/ *的str行的另一个变量** 2 *** /,类似这样:


And I've got 0;

But if I try to assign value of z to another variable at str line marked as /***2***/, something like this:

int y = z;



比我在标记为/ *** 1 *** /

的行上有正确的z值这是为什么会发生这种情况?

在什么情况下,z的值在for循环中输出为0?


than I've got right z value at the line marked as /***1***/
Why is this happening?
And in what cases the value of z is outputed as 0 inside "for" loop?

推荐答案

CPallini是对的:这是 IntToStr 。我可以告诉你更多:那肯定是那里的错误。原因如下: z 没有办法获取任何错误的值,因为它的值完全由的行确定声明头。 (好吧,人们遇到了一些试图修改循环变量的极端变态,而C ++编译器 - 真可惜! - 可以允许这样做,但在你的代码示例中没有任何坏事迹象。)



同时,我们在您的代码示例中看不到 IntToStr 的定义。因此,问题在于该功能。 ∎



:-)



-SA
CPallini is right: this is a bug in IntToStr. I can tell you more: it is certainly the bug there. Here is why: there is no a way for z to get any wrong value, because its values are fully determined by the line with for statement head. (Well, people faced some extreme perverts who tried to modify the loop variable, and a C++ compiler — what a shame! — can permit that, but there is no a sign of such a bad thing in your code sample.)

At the same time, we cannot see the definition of IntToStr in your code sample. Therefore, the problem is in that function. ∎

:-)

—SA


这篇关于读取循环变量时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆