I'm learning C and using CodeBlocks on MacOSX to write and build my test scripts. The instructor says that you cannot increase the elements of an array once set using varname[limitInt].
However, I have the following:
#include <stdio.h>
int main() {
int num[4] = {1,2,3,4,5};
int n = 5;
num[n] = 6;
num[6] = 7;
for(n=0;n<10;n++) {
printf("%d\n",num[n]);
}
}
Building shows warnings about excess elements in array initializer, and also array index 6 is past the end of the array. But I'm not stopped... The build works and I can run with the result of:
1
2
3
4
-353028040
6
7
-1687410582
-353028040
32766
Though it does throw [1] with an abort, it runs.
Here is what I'm thinking:
- The compiler I'm using doesn't gate this, but it should.
- My instructor shouldn't be using language like "can't ever extend an array", as that's clearly possible from my tests (of course only if we ignore the abort and return of 1 to stdout).
For 1: Is there a way to hard fail on building with these types of warnings? Maybe use a different compiler?
For 2: If C is allowing it to extend, but returning 1, can someone describe to me why?