C array setting of array element value beyond size of array

后端 未结 3 427
孤独总比滥情好
孤独总比滥情好 2020-12-19 18:43

I Have this C code snippet

int numbers[4]={1};

numbers[0]=1; numbers[1]=2; numbers[3]=3; numbers[10]=4;
printf(\"numbers: %d %d %d %d %d %d\\n\",numbers[0],         


        
3条回答
  •  死守一世寂寞
    2020-12-19 19:24

    To answer your questions:

    1. Not necessarily. The compiler could reserve memory in larger chunks if you declared the array statically or you could have just overwritten whatever else comes after the array on the stack.
    2. That depends on the compiler and falls under "undefined behaviour".
    3. You set (numbers + 10) to the value after the equal sign.

提交回复
热议问题