[comp.lang.c] Const Usage...

avery@netcom.UUCP (Avery Colter) (12/02/90)

Steven W Orr wrote:

> Where do const keywords fit into this scheme? All I want to do is be
> able to say something outrageous like
> X is a constant pointer to a function that returns a constant
> pointer to a constant array of constant ints.

How could a function return a constant???
Hmmmm. 

All I can figure is that the consts would only be applicable to
the declaration of other variables which will be used to dereference
the variable above.

I mean, this looks like: const int (*(*x)())[].

I like to use backward thinking with declarations, it helps me
visualize what is actually being said. So, ints are the type of
values in an array pointed to by the result of a function pointed
to by x.

I just tried running const int (* const (* const x)())[] through my
compiler. It didn't take. So I can only gather that const will only
be accepted at the start of the line or immediately after the outermost
type.

I think that the const marks x as being constant, regardless of
what type it ultimately is. I still don't think you can have a
function returning a const. I don't think you need to either,
as parameters to a function are never changed on the level of the
program that is calling that function, and returning a const means
that the value returned by the function is always the same (at least
that's the only way I can see to actually do this). Thus, to achieve
that step, you would have the constant pointer point only to functions
which are going to always return the same value regardless of the
input parameters. What is being returned is a pointer to an array.
Now, what is a constant array? There is no real meaning to this, as
an array always has the same number of legal elements. Does a constant
array mean all its elements are constant? Then saying "a constant array
of constant ints" is redundant.

Now, if the leading const makes x itself constant, then there is the
question of how one binds the elements of the array to be constant.
One might have to create the constant arrays of ints separately,
and link x to them later.
-- 
Avery Ray Colter    {apple|claris}!netcom!avery  {decwrl|mips|sgi}!btr!elfcat
(415) 839-4567   "I feel love has got to come on and I want it:
                  Something big and lovely!"         - The B-52s, "Channel Z"

thorinn@rimfaxe.diku.dk (Lars Henrik Mathiesen) (12/03/90)

avery@netcom.UUCP (Avery Colter) writes:
>Steven W Orr wrote:
>> X is a constant pointer to a function that returns a constant
>> pointer to a constant array of constant ints.

>How could a function return a constant???

In strict ANSI C, this has no meaning. In GCC C (a very interesting
language) declaring a function as const means that the programmer
guarantees that it has no side effects and that the result depends
only on the arguments (not even on what they point to).

>I just tried running const int (* const (* const x)())[] through my
>compiler. It didn't take.

With GCC,
	int const (* const (* const X)(void))[100];
is perfectly acceptable. If given the -ansi -pedantic options, though,
it complains that
	x.c:1: warning: function declared to return const or volatile result
However,
	int const (* (* const X)(void))[100];
is perfectly good ANSI C.

The second declaration above means: The identifier X shall stand for
an object. The contents of this object may not be changed. The
contents of the object is the address of a function. This function,
when called, will return a pointer value. This value is the address of
an array of one hundred objects. The contents of these objects may not
be changed. Each of the objects contains an integer.

The description above is somewhat more verbose than usually seen, but
is was derived as usual: By going from the identifier outwards.

I placed the first const after int to emphasize another point: Const
is a property of a variable (object), not of an integer. Also, this
way of thinking makes qualified pointers easier to understand:
	int const ic;
declares a constant object containing an integer,
	int * const ipc;
declares a constant object containing a pointer to an object
containing an integer,
	int const * icp;
declares a object containing a pointer to a constant object
containing an integer, and so on. From this point of view, it is not
logical that type qualifiers are common for all declarators in a
declaration.

Finally, const arrays: The standard declares that if such an array is
declared (using typedefs) const applies to the elements instead.
Indeed, modifying an element of an array is the same as modifying the
array itself, so it would not make sense to distinguish the two
situations. Likewise, GCC seems not to distinguish between the
qualifiers of a function type and the qualifiers of its return type.

>Now, if the leading const makes x itself constant, then there is the
>question of how one binds the elements of the array to be constant.
>One might have to create the constant arrays of ints separately,
>and link x to them later.

As you see, the leading const in fact applies to the array elements.
The only thing in a declaration that always applies to the declared
names is the storage class.

--
Lars Mathiesen, DIKU, U of Copenhagen, Denmark      [uunet!]mcsun!diku!thorinn
Institute of Datalogy -- we're scientists, not engineers.      thorinn@diku.dk