Function pointer casts in ANSI C

Doug Gwyn gwyn at smoke.brl.mil
Fri Nov 30 07:29:10 AEST 1990


In article <1990Nov29.110114.21565 at diku.dk> thorinn at skinfaxe.diku.dk (Lars Henrik Mathiesen) writes:
>My question is: Can a conforming compiler generate a warning for casts
>(and arguments) that will ``go wrong'' but not for those that work?

A conforming implementation must produce at least one diagnostic for each
translation unit that violates a syntax rule or constraint.  Diagnostics
may be produced under other circumstances, but are not required.

In your example, the syntax is presumably correct, so that leaves only the
question of whether a constraint is violated.  The relevant constraint
appears to be in 3.3.2.2 (Function Calls): each argument must be
assignment-compatible with the corresponding parameter.  This leads us to
3.3.16.1 (Simple Assignment), where the constraints require pointers to
compatible types (apart from the qualifiers).  That leads to 3.1.2.6
(Compatible Type and Composite Type), which immediately leads to 3.5.4
(Declarators), more specifically 3.5.4.3 (Function Declarators (Including
Prototypes)), where inder Semantics we finally find out what is really
required: return value and parameters must have compatible types.  So,
the final constraint to be checked is whether void* and data* are
compatible types.  This leads us around to 3.5.4.1, which requires that
void and data be compatible types.  The search ends here, because there
is nothing in the standard that says that void is compatible with a
structure type.

Thus a constraint IS violated, and a diagnostic IS required.  The
compiler could go ahead and translate the source into object code that
might actually work; there is no requirement that a conforming
implementation reject programs that generate diagnostics.



More information about the Comp.lang.c mailing list