Me and a friend of mine are arguing about that so we thought
here we can get an appropriate answer, with the corresponding explanation.
int a[0];
is a[0]
an array and does it have any advantages?
(I didn't only want to know what happened if I defined a and a[0] but also want to know the advantages and how it was more than just the variable.)

- 189
- 1
- 1
- 8
-
I would say that in your case a is just valid as a pointer, since you cannot use any index on it – njzk2 Jun 02 '14 at 13:32
-
3This is an interesting enough question, but it really doesn't show much effort, and the title's misleading. An answer to the title question would, "of course, a[0] is the first element of the array, whereas a is the array. They have different types" etc. There are some close votes (as duplicates) that point out that a zero-size array is not permitted. – Joshua Taylor Jun 02 '14 at 13:32
-
Advantages over what? And what is the context of your variable `a`? – David Heffernan Jun 02 '14 at 13:35
2 Answers
As a standalone stack variable, it is not useful. However, as the last member of a struct, it can be used as a variable length array and doing this used to be a common technique. For example:
struct foo {
int a[0];
};
struct foo* bar = malloc( sizeof *bar + 10 * sizeof *bar->a );
/* now you can (possibly) use bar->a as an array of 10 ints */
Note that this is a non-standard, non-portable hack and is certainly not good practice in new code.

- 204,365
- 48
- 270
- 300
-
Some corrections, if I may: variable length array is something different. It is usually called flexible array member, as defined in C99 standard. Also, `[0]` is GNU C extension, C99 defines it as `[]`. And `sizeof(bar->a)` is 0 in that particular case, it should rather be `sizeof(bar->a[0])`. – keltar Jun 02 '14 at 13:42
-
@keltar: thanks for the comments. I edited to write `*bar->a`, as originally intended. Possibly `bar->a[0]` is more readable. – William Pursell Jun 02 '14 at 13:45
-
Aha! So it does have a use as a variable length array! Thanks for the answer! Might stop arguing soon hopefully. – TheMatrix Jun 02 '14 at 13:47
-
3@TheMatrix this (without the `0`) is *flexible array member*. VLA means something else (block-scope array with size specified by a variable) – M.M Jun 02 '14 at 13:54
YES, they are different according to their functionality. Lets first get into their memory address.
int a[0];
printf("%p / %p", &a[0], &a);
As you can test a[0]
uses the same address as a
.
That means they are the same, but that doesn't mean their behaviour is the same.
That because &a
aways points to a[0]
which is the only one memory area allocated here.
However.. despite the logics, a[0]
IS considered array.
Many people say that an array needs to have more then 1 memory block used, but thats wrong.
And that is because of the index operator []
which is what actually makes something an array. (systematic arrangement of objects)
Even if the object is one as it appears in here a[0]
it is accepted as an array by the compiller and the debug. And you will have to
initialize the array in case you want to assign value INTO it.
Valid:
int a[] = {1};
Valid:
int a[0] = {1};
Invalid:
int a[0] = 1;
On top of that, converting a pointer to the array is valid as follows:
int a[0];
int* p;
p = a;
However you can't just set p to point into a one block of memory, it is reserved for the datatype. At the end we have an integer that points to itself.
The conclusion is that the compiler/debug and you by yourself can threat it like an array, but you can't use it.
(Or at least to expect propper result)
All that means that the difference between a
an a[0]
is the ability of a
to point to its address.

- 2,312
- 1
- 16
- 33
-
-
3`int a[0];` is illegal in Standard C. If you are talking about a particular compiler's extension, please include which compiler and version you are talking about – M.M Jun 02 '14 at 13:41
-
Game-Editor's compiler for e.g, probablly the same compiler he is using, judging by his ex profile description. – Edenia Jun 02 '14 at 13:47
-
I don't care about how a[0] means more than a in game editor. Well, this answer is really detailed and tells most of the uses of how a[0] is more than just a! So 'a[0]' is an array whilst 'a' is a pointer... Thank you for the answer, really helpful. – TheMatrix Jun 02 '14 at 14:03
-
I look'd at your profile description, you said you are developing under legendary compillers. – Edenia Jun 02 '14 at 14:04