🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.c
Article #9559

Re: empty array declarations

#9559
From: Ray Butterworth
Date: Tue, 14 Oct 1986 17:44
59 lines
2340 bytes
> 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".
Technical Details
Message-ID: <1370@watmath.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!rbutterworth
References: <252@whuxcc.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site watmath.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!rbutterworth
From: rbutterworth@watmath.UUCP (Ray Butterworth)
Newsgroups: net.lang.c
Subject: Re: empty array declarations
Message-ID: <1370@watmath.UUCP>
Date: Tue, 14-Oct-86 13:44:24 EDT
Article-I.D.: watmath.1370
Posted: Tue Oct 14 13:44:24 1986
Date-Received: Wed, 15-Oct-86 01:14:10 EDT
References: <252@whuxcc.UUCP>
Distribution: net
Organization: U of Waterloo, Ontario
Lines: 59