[comp.lang.c] Teaching const: decoding declarations

chris@mimsy.UUCP (Chris Torek) (04/10/88)

-In article <27071@amdahl.uts.amdahl.com> nw@amdahl.uts.amdahl.com
-(Neal Weidenhofer) writes:
->	int * const a;
->decodes as:
->	a is a constant,
->	a is a constant pointer,
->	a is a constant pointer to an int.
->		(i.e., a cannot be modified but *a can.)

In article <9683@ism780c.UUCP> news@ism780c.UUCP (News system) writes:
-Note that:
-	 int a[1];
-decodes as:
-       a is a constant,

Nope.

-       a is a constant pointer,
-       a is a constant pointer to an int.
-	       (i.e., a cannot be modified but *a can.)
-
-But there must (?) be some difference between the two.  How do you teach
-this?

Start with the right expansion:

	a is an array
		      1
			of int
		(i.e., `a' used in rvalue contexts is an object of
		type pointer to int, but a cannot be modified because
		it is an array).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

decot@hpisod2.HP.COM (Dave Decot) (04/10/88)

> >	int * const a;
> >decodes as:
> >	a is a constant,
> >	a is a constant pointer,
> >	a is a constant pointer to an int.
> >		(i.e., a cannot be modified but *a can.)
> 
> Note that:
> 	 int a[1];
> decodes as:
>        a is a constant,
>        a is a constant pointer,
>        a is a constant pointer to an int.
> 	       (i.e., a cannot be modified but *a can.)
> 
> But there must (?) be some difference between the two.  How do you teach
> this?

The first declaration requests space for a pointer to an integer. If this is
a static declaration, the value is initialized to 0, so a can never point
to an object, and *a cannot be used to store a value.  Otherwise, the
(automatic) variable gets a garbage initial value which cannot be set to
anything useful.

The second declaration requests space for an array of one integer
(except when used to declare a formal parameter) and *a can immediately
be used to store an integer.

Dave Decot
hpda!decot