6 Years
Discussion Span
Last Post by rubberman

The code you posted is illegal, should not compile (if it does, go complain to your compiler-vendor).

When you declare an array, with a line like this:

int a[5];

Then, the number between the brackets is the total number of elements in the array (in the above, it's 5).

When you address a specific element of the array, as so:

a[0] = 42;

Then, the number between the brackes is the index of the element that you are accessing. In C/C++, and in most other programming languages (except Matlab and Fortran, ASAIK), the first element of the array is the element 0, and the last element would be element n-1 (n being the total number of elements in the array). This is why you will normally see for-loops written like this:

for(i = 0; i < n; ++i)  //note that at the last iteration, we have 'i == n-1'
  a[i] = 42;

The reason why your code is invalid is simply because C does not allow a zero-size array to be declared (because that's impossible). In C++, zero-size arrays are fairly common as a way to trip the compiler into producing a compilation error if you try to do something that shouldn't be done, in C however, there isn't much use for that.


//This code will be compiled successfully with gcc compiler & Actually ,
//my question is not about the array index Zero . i.e Array size Zero.

include <stdio.h>

int main()
int a[0]; // Here , i am declare array with Zero size.
int b=2;
return 0;


Yes, in theory you can have a zero-size array, but you cannot do anything with it except get its address! And, even though conformant compilers will deal with it, it may actually have no address since it uses no space! My guess is that the compiler simply eliminates it as unneeded code/data. However, you are taking its address in your printf() functions, so I guess in this case, it does give you an address. However, I think that the address of a and b (both on the stack) will be the same... Anyway, this begs the question - why are you doing this?! :rolleyes:

Votes + Comments
completely agree

And FWIW, using a zero size array as the last element in a structure is not an uncommon (but deprecated and discouraged practice) for a variable size element who's size you don't know until the structure is created. In such a case, there is usually a member of the structure that stores the size of the array, as in:

struct foo
    size_t sizeOfArray;
    char array[0];

struct foo* makeFoo(size_t size)
    struct foo* retval = calloc(1, sizeof(struct foo) + size);
    retval->sizeOfArray = size;
    return retval;

These days you would use a size of 1 for the array as some compilers don't like 0 size arrays. Remember, just because GCC and/or MS Visual C/C++ let you do so, doesn't mean that other compilers will. Trust me that this exact thing has bitten me in the backside in the past... :-)

Edited by rubberman

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.