"array" vs. "&array" ?

Martin Weitzel martin at mwtech.UUCP
Sat Dec 23 08:04:13 AEST 1989


Some days ago I posted a question if it is legal (or makes sense)
to write a "&" before an array. I received a few responses, some
mentioned, that an "&" before an array is illegal and/or that the
compiler simply ignores it.

I will not object to this - my compiler with ISCs 386/ix also ignores
it - but I will present you a fragment of C-Code, which can not be
compiled (without warnings) with this type of compilers.

To my understanding, the following *is* a type mismatch:
-----------------------------------------------------------------------
main()
{
	char a[10], (*p)[10];

	p = a;
warning: illegal pointer combination, op =
}
-----------------------------------------------------------------------
The compiler seems to take my point of view.

Then I'll try to get it right (to my understanding):
-----------------------------------------------------------------------
main()
{
	char a[10], (*p)[10];

	p = &a;
warning: & before array or function: ignored
warning: illegal pointer combination, op =
}
-----------------------------------------------------------------------
Now, what's that? I've made it right, then the compiler throws out
my "&" and complains that it is wrong. Of course it is wrong now,
the compiler just *made* it wrong!

And to all of you who want to now, why I might want to do such strange
things, look at the following:
-----------------------------------------------------------------------
main()
{
	char m[20][10], (*p)[10];

	p =  m[0];
	/*  ^ to "&" or not to "&", that is the question */
	
	/* do something with (*p)[x] -- a single char
	   and have the possibility to increment p to
           point to the next group of 10 char-s */
}
-----------------------------------------------------------------------
or another one:
-----------------------------------------------------------------------
char (*foo())[10]
{
	static char m[20][10] = {
		/* some initialization */
	};
	int i;
	/* do some calculations giving a value to i */
	return  m[i];
	/*     ^ to "&" or not to "&", that is the question */
}
-----------------------------------------------------------------------
I simply can't get it right! What am I missing?

Or is the compiler, that ignores the &, something missing?

(OK, before you post it: I *know* that I can supress the warning
with a typecast, but I think typecasts should only be used as
last resort and not be necessary if you want nothing more than
a perfectly legal and reasonable assignment or function return!)
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list