Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!dkuug!freja.diku.dk!rimfaxe.diku.dk!thorinn From: thorinn@rimfaxe.diku.dk (Lars Henrik Mathiesen) Newsgroups: comp.lang.c Subject: Re: Const Usage... Message-ID: <1990Dec2.222343.11366@diku.dk> Date: 2 Dec 90 22:23:43 GMT References: <17858@netcom.UUCP> Sender: news@diku.dk (The Netnews System) Organization: Department Of Computer Science, University Of Copenhagen Lines: 67 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 Brought to you by Super Global Mega Corp .com