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],
What you can do is this :
int empty[100];
int numbers[4]={1};
int empty2[100];
memset(empty, 0xCC, sizeof empty);
memset(empty2, 0xDD, sizeof empty2);
numbers[0]=1;numbers[1]=2;numbers[3]=3;numbers[10]=4;
printf("numbers: %d %d %d %d %d %d\n",numbers[0],numbers[1],numbers[3],numbers[6],numbers[10], numbers[5]) ;
Now you can understand what you are overwriting when accessing out of your numbers array
To answer your questions:
It doesn't cause any errors because it is decayed to a pointer arithmetic.
When you write numbers[10]
, it is just numbers + 10 * sizeof(numbers)
, which is fairly correct.
It's undefined behavior to access memory you're not meant to (not allocated for you), so every index out of bound that you access is garbage, including 0
.
Accessing indexes greater than 4 will not increase the array's size, as you said, and additionally, it does not do anything either.