empty array declarations

Ray Butterworth rbutterworth at watmath.UUCP
Wed Oct 15 03:44:24 AEST 1986


> Can anyone tell me why the following program is NOT in error?
> Can anyone tell me what it MEANS?
>     main()
>     {
>         int a[];
>         a[0] = 5;
>     }
> I was recently bitten by code like this, and was wondering why it isn't
> illegal to do such things.

It should be illegal.
Here's a change to the 4.2 source for /usr/src/lib/mip/pftn.c:

>  fixtype( p, class ) register NODE *p; {
>  ...
>      if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) )
>          class = PARAM;
>  #ifdef LINT    /* actually it should be a compiler error */
>      if (ISARY(type)){
>          if (dimtab[p->fn.cdim]==0){
>              if ((class==AUTO)
>               || (class==REGISTER))
>                  uerror("illegal null dimension array");
>          }
>      }
>  #endif /*LINT*/
>      if( class == PARAM || ( class==REGISTER && blevel==1 ) ){
>          if( type == FLOAT ) type = DOUBLE;
>          else if( ISARY(type) ){
>  #ifdef LINT
>              if (dimtab[p->fn.cdim]!=0)
>                  werror("array[%d] type changed to pointer",
>                      dimtab[p->fn.cdim]);
>  #endif /*LINT*/
>              ++p->fn.cdim;
>              type += (PTR-ARY);
>          }

The first change will complain about your example.
It probably should affect the compiler too
(i.e. get rid of the "#ifdef LINT").

The second change will warn about things like

>  func(a)
>      char a[17];
>  {
>      char b[17];
>  
>      if (sizeof(a)==sizeof(b))
>          printf("this machine has weird pointers\n");
>  }

If you really want to be daring, get rid of the "if (dimtab...!=0)"
in the second change.  Then it will complain about things like
   func(a)
       char a[];
   {
which really should be "char *a".



More information about the Comp.lang.c mailing list