function prototype problem
Stephen J. Friedl
friedl at vsi.COM
Tue Nov 15 03:13:54 AEST 1988
In article <310 at drd.UUCP>, mark at drd.UUCP (Mark Lawrence) writes:
> I compile the following code fragment:
>
> char *rpeet ( short, char ); /* just a declaration */
>
> char *rpeet ( num, ch ) /* actual definition */
> short num; char ch;
> {
> /* foo */
>
> and get the following gripe from my compiler (gcc 1.30):
>
> rpeet.c:9: argument `num' doesn't match function prototype
> rpeet.c:9: argument `ch' doesn't match function prototype
This is a gotcha that is not obvious -- it should be of general
interest to all using a dpANS compiler.
This compiler message is correct. In an old-style function
declaration, the *actual* types of the arguments are those that
would result from the default argument promotions (char-->int,
short-->int, float-->double). So the above is the same as if the
user had written:
char *foo ( short, char );
vvv------------------- NOTE `int'
char *foo ( num, ch ) int num, ch;
{
/* foo */
which is a mismatch. The solution is to either declare both args
to be int everywhere or to use the prototype format for the function
definition.
The standard says [p 66, May88 dpANS]:
3.5.4.3. Function declarators (including prototypes):
[....]
If one [function declaration] type has a parameter-list
type and the other type is specified by a function
definition that contains a (possibly empty) identifier list,
both shall agree in the number of parameters, and the type
of each prototype parameter shall be compatible with the
type that results from the application of the default
argument promotions to the type of the corresponding
identifier.
----
Followups to comp.std.c
Steve, the stumbling lint (Hi Karl!)
--
Steve Friedl V-Systems, Inc. +1 714 545 6442 3B2-kind-of-guy
friedl at vsi.com {backbones}!vsi.com!friedl attmail!vsi!friedl
------------Nancy Reagan on the worm: "Just say OH NO!"------------
More information about the Comp.lang.c
mailing list