Is it legal to keep the size of an array 0. gcc 3.4.2 did not give any
error for the declaration
int a[0];
This is a gcc extension (though many other compilers support it too), so the
answer in general is "no, this is not legal". You have to supply -pedantic
before gcc will even think of complaining, however.
If the declaration is legal, then what will be the implications of
saying
a[0] = 10;
Will it lead to memory corruption??
Merely declaring an array of size 0 is undefined behavior, so the assignment
wouldn't matter.
If C allowed 0-sized arrays, though, then a[0] would be out of bounds, and
the assignment produces undefined behavior. In short, anything may happen,
including nothing, a compiler error, an OS trap, stack corruption or,
indeed, demons flying out of your nose.
However, the most common application of zero-sized arrays is to provide an
alias for a pointer to an object of unspecified length, allocated by some
unspecified means. This object may happen to be of the appropriate type, in
which case the assignment will do what is expected.
S.