"array" vs. "&array" ?

Chris Torek chris at mimsy.umd.edu
Fri Jan 12 02:41:25 AEST 1990


In article <CHUCKP.90Jan10103228 at halley.ncr-fc.FtCollins.NCR.com>
chuckp at ncr-fc.FtCollins.NCR.com (Chuck Phillips) writes:
>Granted.  But the question remains: "What is the compiler supposed to do when
>the programmer askes for a pointer to something in the symbol table?"

The language does not provide a means for the programmer to do such a
thing (the language makes no statements about the internals of compilers,
or even the existance of compilers, other than to require anything that
pretends to implement C to follow some particular standard).

>Personally I like the idea of hysterical electronic laughter, but was asking
>what ANSI had specified.

I think the case in question here is supposed to be this:

 1	float arrf[3] = { 1.2, 2.3, 3.4 };
 2
 3	foo() {
 4		float **p;
 5
 6		p = &arrf;
 7	}

The question is `What is the appropriate response from a C implementation
when it is fed this code?'; the answer is `Produce a diagnostic.'  (In
other words, `hysterical electronic laughter'.)

Line 7, in particular, consists of:

  (/)	<object, pointer to pointer to float, p> =
 (//)		& <object, array 3 of float, arrf>;

The LHS of the equal sign (marked `/') is in object context, hence
remains unchanged.  The RHS (`//') is in value context, but is
complex.  The first part is a unary `&', whose target is in object
context:

	& <object, array 3 of float, arrf>

Since it *is* an object, the `&' applies.  The result is a value:

	<value, pointer to array 3 of float, &arrf>

This is a value in a value context, so it now remains unchanged.
We now have

	<object, pointer to pointer to float, p> =
		<value, pointer to array 3 of float, &arrf>;

An assignment is correct ONLY IF THE TYPES OF THE LEFT AND RIGHT
AND SIDES MATCH.  Here they do not.  A compiler must issue a
diagnostic.  This is either an error: the line is ignored, or
a warning: the compiler makes a guess as to what the programmer
intended.  Many compilers follow Stephen Johnson's lead by making
it a warning, and inserting a cast:

	<object, pointer to pointer to float, p> =
insert>	    (float **)
		<value, pointer to array 3 of float, &arrf>;

The result of this cast is machine dependent.  In order to decide
what it does, we have to be told what machine (and possibly which
implementation of the C language on that machine) is being used.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list