pointer to array

ed gokhman esg at mtx5a.UUCP
Sat Mar 8 01:57:22 AEST 1986


>    z = &x is illegal although the types would match

No types do not match here:
	(int (*)[5]) != (int **)

>    z = &y[2] is illegal, for no good reason (my opinion)

There is a good reason:
	(int (*)[5]) != (int **) 

There are also quite a few strange sizeofs like:
>    sizeof &*(*z.r) == 4 if pointers are 4 bytes
what meant, I think, was 
	sizeof &*(*z).r


At any rate, a valid point was made. I have another example.
Although, the logic of language interpretation is understandable,
the result is not elegant:

typedef int	foo[5];
foo	a;

main()
{
	funct( a );
}
int
funct ( b )
foo b;
{
foo c;
	printf("%d\n", sizeof b);	/* result is 4 if ptr is 4 */
	printf("%d\n", sizeof c);	/* result is 20 */
}

I think, a compiler should appreciate the fact that b
was not declared as int * but as int [5] and give a
correct size of declared object. Otherwise, it
simply should not allow a declaration of a formal parameter
as an array.

Similar situation may be observed in the new C compilers,
where formal parameter may be declared as a function but
will be interpreted as a pointer to a function.

Another example, where C could be more elegant is the
following:

file1.c:
	int	a[5];
	...

file2.c:
	extern	int	*a;
	...

This one compiles fine, but just watch out at run-time !

I think, that "intimate relationships between arrays and pointers"
should be cool off a little bit for the sake of stronger typing.

				Ed Gokhman



More information about the Comp.lang.c mailing list