The cause of the problem is likely gcc "gnu mode" (gnu89) which is the default compiler setting on older compilers. It will not compile your code according to the C standard, but according to a non-standard invented by a gnu. Therefore your severe bugs passed.
If you instead compile the code as C language, using -std=c89 -pedantic-errors
, you get the following list of diagnostic messages:
error: ISO C forbids zero-size array 'array' [-Wpedantic]|
warning: missing braces around initializer [-Wmissing-braces]|
warning: (near initialization for 'array[0]') [-Wmissing-braces]|
error: excess elements in array initializer|
error: (near initialization for 'array')|
The cause is as already mentioned, that array size during declaration is not the same as array indexing during access. When fixed & compiled as C language, it compiles cleanly and runs correctly:
#include <stdio.h>
int main(int argc, char *argv[])
{
int array[1][4] = { {1, 2, 3, 4} };
int i;
for (i = 0 ; i < 4 ; i++)
{
printf("%d\n", array[0][i]);
}
return 0;
}