The fact that you get one, and always one NULL value is due to the content of the stack, which depends on the code executed before your main gets called, which in turn depends on the compiler/libc you are using, and on the compilation options.
But once compiled, running the programm any number of times will always get you the same values.
As stated in other answers, this is because you did not explicitely initialise the array and it's scope (local to the function) allows the compiler to place it on the stack, where it gets the content of what was there before the call to main.
To get 6 NULL pointers, you should either explicitely initialize the array (int *array[6] = {0};
) or make it static or global.
In the later cases, the compiler will place the array in the bss section (data section which is not present in the binary, but which get allocated and initialised to zeroes by the libc before calling your main() function).
In the first case (local array initialised to 0) the compiler can now make optimisations, as the value is known and never modified, and will certainly remove both the array and the test and simply call printf 6 times.
Compiled with another compiler, or with different optimisation levels (-O2, -O3) will gave you a different result, but still consistent (if not identical) between each calls.
Anyway, regardless of initializing your array, you should fix your code regarding to the respect of C standard :
your main must return a value, and ‘for’ loop initial declarations are only allowed in C99 mode, so you should get this :
#include <stdio.h>
int main(){
int* array[6];
int i = 0;
for (i = 0; i < 6; i++) {
if (array[i] == NULL) {
printf("%d NULL\n", i);
}
}
return 0;
}
I added display of the position number of the NULL value, to show that it's always the same one.
You can even add an else statement, and print the values when not NULL :
} else {
printf("%d %p\n", i, array[i]);
And you will get something like this :
$ ./test
0 NULL
1 0x4011cd
2 0x7f8d2fdf64c0
3 NULL
4 0x4011a0
5 0x401050
$ ./test
0 NULL
1 0x4011cd
2 0x7fa96155e4c0
3 NULL
4 0x4011a0
5 0x401050
I could run it 1000 times, values for 0, 1, 3, 4 and 5 are always the same ones. Only the value for number 2 changes, which I think means that it is used to store an address.
One could get exact information by reading the output of objdump -D and analyzing the code generated by GCC.