Array index out of bound in C

后端 未结 10 1137
抹茶落季
抹茶落季 2020-11-21 21:58

Why does C differentiates in case of array index out of bound

#include 
int main()
{
    int a[10];
    a[3]=4;
    a[11]=3;//doe         


        
10条回答
  •  旧时难觅i
    2020-11-21 22:59

    Just to add what other people are saying, you cannot rely on the program simply crashing in these cases, there is no gurantee of what will happen if you attempt to access a memory location beyond the "bounds of the array." It's just the same as if you did something like:

    int *p;
    p = 135;
    
    *p = 14;
    

    That is just random; this might work. It might not. Don't do it. Code to prevent these sorts of problems.

提交回复
热议问题