[net.lang.c] odd typedef summary

mbarker@bbnz.ARPA (Michael Barker) (11/11/85)

(with apologies for the length)

Some time ago, I sent a short test program out on the net for fun - I've
included it again at the end for those of you who missed it the first
time around...

and, here's what you've been waiting for :-)

summary of responses as of 11 Nov 1985

compiles and works

    CPZ-48000 (z-80 cpm 2.2) Manx C                   
    Zenith Z-120 MS-DOS 2.11 Lattice C 2.14           
    Perkin-Elmer 3220 running v7                      
    TOPS-20 (DEC-20) v7 compiler from New Mexico Tech 
    BBN C-70 v7 compiler                              
                                                      
Fails to compile (error type, then the list)
                                                                               
    "ips.c", line 23: compiler error: compiler takes alignment of function     
                                                                               
    VAX ULTRIX                                                                 
    VAX VMS 3.7  ??? C                                                         
    AT&T 370 C compiler running on CMS                                         
    Sun workstations with Sun OS 2.0 (a modified 4.2BSD)                       
    Masscomp                                                                   
                                                                               
    Line 10: ; expected (points to first character of fist in the ford fist    
    declaration)  Numous other error messages, including declaration mismatch  
    at line 22.                                                                

    Microware os9/6809 cc1:                                                    
                                                                               
    line 24: ; where other token expected (; at end of printf!!!!)             

    Prime C 1.1-19.1:                                                          
                                                                               
thanks to:

    Pawka <PAWKA at NOSC-TECR>                           
    Bob Larson <BLARSON%ECLD@ECLA>                       
    "Josh Knight"   <JOSH%YKTVMH.BITNET@WISCVM.ARPA>     
    Scott J. Kamin <ihnp4!onecom!sjk%seismo.UUCP@bbnccv> 
    Dave Sherman <ihnp4!utzoo!lsuc!dave@Berkeley>        
    Terry Poot <ndm20!tp%smu.csnet@CSNET-RELAY.ARPA>     
    Greg Titus <GREG@pinon.tccnet>                       
                                                         
    While I'm not a compiler expert, five systems saying yes and seven
saying emphatically no seems like a problem.  I think the ones saying no
may be derivatives of PCC, while the others seem more likely to have
"homebrew" compilers (no derogative intent, I assure you).  Anyone
feel like raising this issue with the ANSI C folks?  Incidentally, our
local compiler experts say it should be ok, while various other people
have had (of course) various opinions.

    Thanks again
    mike

ARPA: mbarker@bbnz.ARPA
UUCP: harvard!bbnccv!mbarker
------------(repeat of previous message, edited for brevity)-------------
two relatively simple (I hope) questions:

1.  please test the following program on your compiler.  Does it work?
2.  should it?

this is what a run of the program gives:
before use 1
that's the rub
after use 3

this is what lint had to say
"ips.c", line 23: compiler error: compiler takes alignment of function
----------------------------cut here-------------------------------
/* test program to show quirk in typedef's
 */

#include <stdio.h>

typedef ford();         /* this is questionable statement 1 */

main()
    {
    ford fist;          /* declare the function? question 2 */
    int some;           /* just filler */

    some = 1;

    printf("before use %x\n",some);

    some = fist();  /* use it */

    printf("after use %x\n",some);
    }

ford fist               /* see, mom, no parentheses? question 3 */
    {                   /* this is line 23 ??? */
    printf("that's the rub\n");

    return(3);          /* simple, eh? */
    }
-----------------------(that's all, folks)-------------------------

danny@nvzg2.UUCP (Danny Zerkel) (11/13/85)

>
> /* test program to show quirk in typedef's
>  */
> 
> #include <stdio.h>
> 
> typedef ford();         /* this is questionable statement 1 */
This is fine, and legal.
> 
> main()
>     {
>     ford fist;          /* declare the function? question 2 */
This again, is fine.
>     int some;           /* just filler */
> 
>     some = 1;
> 
>     printf("before use %x\n",some);
> 
>     some = fist();  /* use it */
> 
>     printf("after use %x\n",some);
>     }
> 
> ford fist               /* see, mom, no parentheses? question 3 */
>     {                   /* this is line 23 ??? */
> "ips.c", line 23: compiler error: compiler takes alignment of function
This is where the trouble begins, and is flagged correctly by the compiler.
The effect of the typedef and it's use in this context is to tell the
compiler that the initialization of function fist (which returns an int)
begins here.  However, initialization of functions is a special case in
C.  The syntax of data and function initialization (at least originally)
is identical [by design, it makes C look more orthoganal than it is], the
semantics are different however.
>     printf("that's the rub\n");
> 
>     return(3);          /* simple, eh? */
>     }
>

In the C compilers derived from the original Unix C compiler, functions
are only initialized with ().  Note if you change line 22 from:
  ford fist
to:
  fist()
the problem goes away!  If the compiler figured out that you ment to
initialize a function with line 22, and switched contexts, everything
would again be fine.  Data initialization has been changed however, and
it is not clear if a '=' would be required!!

The whole problem is a grey area where data becomes action (a function)
and vice versa.  Realistically C has no (portable) mechanism to manipulate
the contents of functions, so it's probably not necessary to cloud an
already terse language with two forms of function initialization.

PS
  The error is flagged on line 23 because that's when the compiler figured
  out there was something wrong.  It is impossible to figure out what is
  going on from line 22!  (And compilers, like plow-men, don't look back.)

-------------------------------------------------------------------------
From: Danny J. Zerkel ("I call myself, me")
      somewhere called ..nvzg1!danny (which is under nvzg2, if that helps)
      in reality,
	 AT&T-IS, 78Q458b
	 151 Wymore Rd
	 Altamonte Springs, FL 32714
	 (305)869-2743  (ISCOMM 7552743)

		"I think all right thinking people in this country
		 are sick and tired of being told that ordinary
		 decent people in this country are fed up with being
		 sick and tired!"
					- Monty Python

The author is not known to represent or have any special interests.
	"He's just this guy, you know."