#include <stdio.h>
int main()
{
int a[0],b[4][0];
printf("%d %d ",sizeof(a),sizeof(b));
}
//output
0 0
what is the significance of a[0]
, why also 2d array of size 0 is allowed?
#include <stdio.h>
int main()
{
int a[0],b[4][0];
printf("%d %d ",sizeof(a),sizeof(b));
}
//output
0 0
what is the significance of a[0]
, why also 2d array of size 0 is allowed?
Neither C nor C++ allow arrays of zero length, so your program is ill-formed.
(E.g. C++11, 8.3.4/1: "[the array size] shall be greater than zero".)
(As one point of rationale: An array of zero length would be tricky and confusing to reconcile with the requirement that each object have a unique address.)
As @sidyll points out, zero-length arrays are available as an extension in GCC.
You'll find your answer in The GCC manual
If you are using c99
- Flexible array members are written as contents[] without the 0.
- Flexible array members have incomplete type, and so the sizeof operator may not be applied. As a quirk of the original implementation of zero-length arrays, sizeof evaluates to zero.
- Flexible array members may only appear as the last member of a struct that is otherwise non-empty.
- A structure containing a flexible array member, or a union containing such a structure (possibly recursively), may not be a member of a structure or an element of an array. (However, these uses are permitted by GCC as extensions.
And of course, how they can be useful:
Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:
struct line { int length; char contents[0]; }; struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length;
There is not much use as given in your example, but zero size arrays were frequently used in structures where the last element was dynamically sized:
struct {
int some_fixed_data [N_FIXED];
float more_fixed_size_data [F_FIXED];
int n_variable_elements;
long variable_elements [0]; // allocated based on item above
} s;
int curlen = sizeof s + sizeof long * s.n_variable_elements;
The use of a zero length array is:
1) variable_elements
has an address (despite someone's answer)
2) it also has array semantics
3) computing the dynamic size of the array is simplified
Unfortunately, some compilers (MSC) would throw a hissy fit over such a construction and force a lesser, appeasing, technically incorrect reformulation:
struct {
int some_fixed_data [N_FIXED];
float more_fixed_size_data [F_FIXED];
int n_variable_elements;
long variable_elements [1]; // allocated based on item above
} s;
int curlen = sizeof s + sizeof long + (s.n_variable_elements - 1);
Think of a zero size array as a placeholder. There is little need to do that anymore, unless you are forced to use C
which is the case on many embedded environments.