Teaching const: decoding declarations

Dave Decot decot at hpisod2.HP.COM
Sun Apr 10 15:54:40 AEST 1988


> >	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



More information about the Comp.lang.c mailing list