I want to know in c language, if I have an array whose length is 3, if i try to access the 4th element of the array, which memory address will it point to?
I have read similar problems of accessing array element out of bound, they said that this is a typical undefined behavior
which is unsafe, but will there be any common rules for where the 4th element would point to?.
For example, which memory address would the array[3]
refer to with declaration given here?
int a = 10;
int array[3] = {1, 2, 3};
int b = 20;
printf("%d", array[3]); // access the 4th element here
May it point to a
or b
or array[x]
or it's totally random?
The key point here is : if i declared variable A after variable B (especially when they are global variable or static variable), will they be stored continuously in memory? Or it's totally depend on compiler?