Why is there an infinite loop in my program?

后端 未结 5 719
再見小時候
再見小時候 2021-01-04 16:15
int main(void)
{
    int i;
    int array[5];

    for (i = 0; i <= 20; i++)
    array[i] = 0;

    return 0;
}

Why is the above code stuck in a

5条回答
  •  渐次进展
    2021-01-04 16:49

    The problem is when you try to access an element outside the bounds of the array, which is only 5 big - but in a loop that is 21 big.

    int main(void)
    {
        int i;
        int array[5];
    
        for (i = 0; i < 5; i++)
        array[i] = 0;
    
        return 0;
    }
    

提交回复
热议问题