Why does this for loop exit on some platforms and not on others?

前端 未结 14 2759
执念已碎
执念已碎 2020-12-12 09:00

I have recently started to learn C and I am taking a class with C as the subject. I\'m currently playing around with loops and I\'m running into some odd behaviour which I d

14条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 09:53

    the error is in portion array[10] w/c is also address of i (int array[10],i;). when array[10] is set to 0 then the i would be 0 w/c resets the entire loop and causes the infinite loop. there will be infinite loop if array[10] is between 0-10.the correct loop should be for (i = 0; i <10 ; i++) {...} int array[10],i; for (i = 0; i <=10 ; i++) array[i]=0;

提交回复
热议问题