Subject: Re: Question regarding the array of size 0.
To: Peter Seebach <>
From: John Franklin <>
List: tech-kern
Date: 03/20/2001 14:21:27
On Tue, Mar 20, 2001 at 01:03:21PM -0600, Peter Seebach wrote:
> In message <>, Shankar Agarwal writes:
> >Can someone pls tell me if it is possible to define an array of size 0.
> Not in C.

Actually you can (see below).  It depends on the compiler and how strict
you have it checking things.  It only works in this case because the 
memory manager is allocating an entire page for the structure, not
just the size of the structure.

It's not uncommon to use this for message-based communications where you
have a header and payload  and want to use sizeof(struct message) to get 
the header size, but also want to use foo.payload to access the message 
itself.  In that case, it's more likely to be used as a cast on a buffer,
(e.g., ((struct message*) buffer)->payload)

Realize, tho, there's a potential portability issue, if you use this.

What follows was done on a NetBSD 1.5 system.

[24]% cat zero.c && make zero && ./zero
#include <stdio.h>

struct zero_array {
        int header;
        int payload[0];

int main()
        struct zero_array foo;

        printf("Foo:\n\theader: %d\n", foo.header);
        printf("\tpayload 0: %d\n", foo.payload[0]);
        printf("\tpayload 1: %d\n", foo.payload[1]);
        return 0;
cc -O2   -o zero zero.c
        header: 1
        payload 0: 10
        payload 1: 12

John Franklin
ICBM: N37 12'54", W80 27'14" Z+2100'