odd typedef summary (100+ lines)
Danny Zerkel
danny at nvzg2.UUCP
Thu Nov 14 06:26:29 AEST 1985
>
> /* 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."
More information about the Comp.lang.c
mailing list