Take the 2-minute tour ×

I get that using negative indexes is just pure luck. But out of curiousity I tried this. I know you can declare array[0]; just like malloc(0); is legal. But how come I can store a value in array[0]?

#include <stdio.h>
#include <conio.h>
int main(void)
    int i;
    int array[0];
    array[0] = 5;
share|improve this question
i think,u can't store value in that array –  Ravindra Bagale Sep 27 '12 at 11:43
You can write array[1] = 5 as well and the value will be stored. It's just that you are writing into a memory location not reserved for you. –  Vikdor Sep 27 '12 at 11:43
Accessing negative indexes is NOT pure luck, is doable via pointers. –  Paul Sep 27 '12 at 11:49
All of these experiments of yours can be very well cleared if you just debug your code and keep track of your memory. Visual Studio gives a lot of options on these. You can keep a watch on your variables, the registers they access and the memory they write to. Anyways, nice experiments ;-) –  Abhineet Sep 27 '12 at 11:53
How many different ways can you think of to use an out-of-range index, and be told that the result is undefined behavior because the index is out of range, and then ask again about a different out-of-range index? ;-) Granted, in this example you've introduced a new twist that the array definition is illegal, but even if it was legal you'd be accessing it out of bounds. –  Steve Jessop Sep 27 '12 at 12:18

5 Answers 5

up vote 4 down vote accepted

Such a 0 sized array is a constraint violation in standard C, you compiler should not let you get away with this without giving you a diagnostic. If it doesn't tell you something then, that must be an extension that your compiler vendor has added to its C dialect.

Don't rely on such extensions.

Regardless whether or not you'd declare the array with size 0, C has no imposed bound checking of array or pointer access. A good modern compiler should still give you a warning, though, if the excess of the bounds is known at compile time.

share|improve this answer
This is the only correct & complete answer +1. –  Blue Moon Sep 27 '12 at 12:12

You are accessing a memory space that is either undefined or used by something else. This code will eventually screw something up because until you use malloc you have no idea where the array exists. You created an array of size zero. C will let you write whatever you want to wherever you give it an address. Barring the operating system from interfering. "array" is a pointer to an array and it has some arbitrary value. The compiler has no idea that that memory space should be reserved for the zero'th element of array as you are using it.

share|improve this answer
In his example, he cannot use malloc to allocate space for his array. He would have needed to declare int *array; in order to later assign it space (with malloc or other allocation functions). –  mah Sep 27 '12 at 11:51
@mah Yes, that is true. array is still a pointer, though, just not to a place that the compiler will let him use malloc because it is a statically defined array. What I meant to say that is that unless you have a statically allocated array, malloc is the only way of guaranteeing reserved memory. –  Jeremy Sep 27 '12 at 11:54

C assumes (where it can) that you know what you're doing. A zero length array still has an address (but that address can easily be shared with something else of size). When you index into that array, you're just modifying the memory location you're using, without concern for what else uses the address you end up with -- and by writing, you can easily cause huge (and difficult to debug) problems.

share|improve this answer
No zero sized arrays are simply not allowed in standard C. –  Jens Gustedt Sep 27 '12 at 11:50
@Jens ideone.com/7ZT0K -- the compiler allows it and executes it though. Perhaps the compiler is simply optimizing out the array? I do get a segfault trying to print the address of array but if it's not permitted by the standard, it's concerning that it compiles. –  mah Sep 27 '12 at 11:58
It must give you a diagnostic, so definitively this is a non standard compiler. –  Jens Gustedt Sep 27 '12 at 12:00
When compiling this using gcc version 4.4.5 20101112 with -Wall -Wextra the only warning is that variable i is unused. The same results occur when using -std=c99 and/or -ansi. What option do I need to provide to gcc to cause a zero length array to fail to compile? I would like to be able to compile to full compliance rather than relying on unportable extensions. –  mah Sep 27 '12 at 12:51
You'd have to add -pedantic. Gcc then tells me: "ISO C forbids zero-size array" –  Jens Gustedt Sep 27 '12 at 13:23

Just like Vikdor said in the comment, you are writing in a memory location not reserved for you. This might result in serious and hard to debug problems so I suggest you never do this, but this is how it works:

when you declare an array of size 0 int array[0] the complier is going to associate the name 'array' with a memory location. Let's say 100 for this example. But because the size of the array is 0, no byte belongs to the array, so bytes 100, 101 and so on might be allocated to other variables too.

When you say array[0] = 5 you are writing the number 5 to bytes 100, 101, 102 and 103, because an int is 4 bytes long. And then you can read that number using array[0] that reads 4 bytes starting at location 100.

A problem appears when the space starting at 100 is given to some other variable because then it might overwrite array[0] and it will appear as if array[0] had changed for no reason (this is where you will spend lots of frustrating time debugging)

Keep in mind that int array[0] is just like int *array. The name of an array is just a pointer.

share|improve this answer
int array[0]; 

is Invalid C code.It is non-standard conforming. Your code compiles cleanly because it uses a compiler specific extension.

C99 Standard: Array declarators
Para 1:

In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.

Why does the seem to work?

Assuming your compiler implementation allows zero length array's:

array[0] = 5; 

still works because this code statement causes Undefined Behavior.
It writes to a memory region that is not owned by the array thus overwritting the bounds of allocated memory. Luckily, it works because the memory is probably not in use by some other entity. Technically, it is still an Undefined Behavior.

share|improve this answer

Your Answer


