sizeof and array parameters

jsdy at SEISMO.ARPA jsdy at SEISMO.ARPA
Fri Dec 28 11:03:12 AEST 1984


> #define ARRAYSIZE 4
> 
> readarray(fd,a)
>  int fd;
>  int a[ARRAYSIZE];
> {
>     read(fd,a,sizeof(a));
> }
>                        ... sizeof(a) was 4 (the size of a pointer) ...
>  ... I feel that sizeof should have returned 16.  ...
> The question of course, is whether one should view paramter "a" as
> an array, or just a pointer.  My view is that it is an array, and
> should be treated as such.  ...  since it has a dimension
> there is no justification for semantically treating "a" as if it were simply
> a pointer to integer.  Obviously the representation is still a pointer
> to integer, but the major justification for "int a[]" being equivalent
> to "int *a" is simply that we don't know the true size of "a".  ...

There's some justification to this.  But it is generally understood
(K&R 5.3 par.9, e.g.) that the parameter can be handled as either a
pointer or an array.  Everything you can do to an array, you can do
to a pointer, but not vice versa -- so parameters are in fact imple-
mented as pointers.  (E.g., ++a is legal even above.)  It doesn't
matter what convenient legal fiction you are using at the moment.
The compiler must recognise 'a' as a pointer:  if it treats it as
an array, you will be climbing the stack instead of dereferencing
the pointer.

Joe Yao		(UUCP!seismo!hadron!jsdy / hadron!jsdy at seismo.ARPA)



More information about the Comp.lang.c mailing list