[comp.std.c] compatibility of types

rpjday@ccu.umanitoba.ca (07/11/90)

  Can someone tell me if ANSI C considers a type of int[3] in
a prototype compatible with a type of int[5] in the function
definition?  And are these compatible with a type of int*?
A reference to the draft would do nicely (this is a subtle
way of saying that I would RTFM if I knew which section of
TFM was what I wanted :-)
  Thanks.

diamond@tkou02.enet.dec.com (diamond@tkovoa) (07/11/90)

In article <1990Jul11.002522.24178@ccu.umanitoba.ca> rpjday@ccu.umanitoba.ca writes:

>  Can someone tell me if ANSI C considers a type of int[3] in
>a prototype compatible with a type of int[5] in the function
>definition?  And are these compatible with a type of int*?
>A reference to the draft would do nicely (this is a subtle
>way of saying that I would RTFM if I knew which section of
>TFM was what I wanted :-)

The answers are yes and yes, because the first two types are adjusted
to be exactly the third type.  Section 3.7.1 states this, and section
3.5.4.3 has a sentence that refers to section 3.7.1.

However, you might want to beware of a subtle difference regarding
some types that you did not ask about.  int(*)[3] and int(*)[5] also
point to integers, but they are not compatible with each other.
It's a bit too subtle for me to figure out if each is compatible
with int* or not.
-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
This is me speaking.  If you want to hear the company speak, you need DECtalk.

gwyn@smoke.BRL.MIL (Doug Gwyn) (07/12/90)

In article <1990Jul11.002522.24178@ccu.umanitoba.ca> rpjday@ccu.umanitoba.ca writes:
>  Can someone tell me if ANSI C considers a type of int[3] in
>a prototype compatible with a type of int[5] in the function
>definition?  And are these compatible with a type of int*?
>A reference to the draft would do nicely (this is a subtle
>way of saying that I would RTFM if I knew which section of
>TFM was what I wanted :-)

It's hard to see how you could not find this:

3.5.4.3 (Funtion Declarators (Incuding Prototypes):  For two function
types to be compatible, ... corresponding parameters shall have
compatible types. ... (For each parameter declared with function or
array type, its type for these comparisons is the one that results
from conversion to a pointer type, as in 3.7.1. ...

rex@aussie.UUCP (Rex Jaeschke) (07/12/90)

> diamond@tkou02.enet.dec.com (diamond@tkovoa) writes:
> 
> However, you might want to beware of a subtle difference regarding
> some types that you did not ask about.  int(*)[3] and int(*)[5] also
> point to integers, but they are not compatible with each other.
> It's a bit too subtle for me to figure out if each is compatible
> with int* or not.

A pointer to an array of x int is NOT assignment-compatible to a pointer
to an array of y int nor to a pointer to an int.  You MUST use an
explicit cast to compare/copy etc.  them or to mix them when calling a 
function in the presence of such a prototype. It is conceivable that a 
pointer to an array might be a "fat" pointer containing information 
about the underlying array size giving it a different representation 
to a straight int pointer.

Rex

----------------------------------------------------------------------------
Rex Jaeschke     |  Journal of C Language Translation  | C Users Journal
(703) 860-0091   |        2051 Swans Neck Way          | DEC PROFESSIONAL
uunet!aussie!rex |     Reston, Virginia 22091, USA     | Programmers Journal
----------------------------------------------------------------------------
Convener of the Numerical C Extensions Group (NCEG)
----------------------------------------------------------------------------

karl@haddock.ima.isc.com (Karl Heuer) (07/12/90)

In article <1842@tkou02.enet.dec.com> diamond@tkou02.enet.dec.com (diamond@tkovoa) writes:
>int(*)[3] and int(*)[5] also point to integers,

Actually they point to arrays which happen to contain integers, but we'll let
that pass.

>but they are not compatible with each other.  It's a bit too subtle for me to
>figure out if each is compatible with int* or not.

No, but they are each compatible with the pointer-to-incomplete-type int(*)[].

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint