[m-dev.] Tool for generating DU types in C

Paul Bone pbone at csse.unimelb.edu.au
Mon Mar 22 22:00:33 AEDT 2010


On Mon, Mar 22, 2010 at 05:59:10PM +1100, Peter Moulder wrote:
> On Mon, Mar 22, 2010 at 03:29:59PM +1100, Paul Bone wrote:
> 
> > You should also make the sentinel object above const so that it is allocated in
> > read-only memory at runtime.  The constructor should also return a const
> > object, use cdecl(1) for help with the const keyword, I never remember the
> > order of these keywards for declarations.
> 
> `const' and other qualifiers `volatile' and (in C99) `restrict' modify
> whatever (non-qualifier) part of the type immediately precedes that qualifier.
> So `char const *' is a non-constant pointer to a constant char.
> 
> As a special case, if there is nothing (other than other qualifiers) preceding
> the qualifier, it modifies the part following it: so `const char *' is also a
> non-constant pointer to a constant char.  However, I'd recommend against using
> that form -- precisely because it's a special case and it promotes confusion
> about what the general rule is for qualifiers.  I've seen more than one
> experienced C programmer confused by const ordering, and I seem to recall
> attention being brought to such an error in mercury-reviews; I believe that
> consistently placing `const' after the thing being modified will reduce such
> confusion.

Thanks!  I consider myself a _very_ good C programmer, but I've never pretended
to understand this, I've simply known that cdecl knows and therefore I don't
have to.  But this explanation is simple and clears it up for me.  Cheers!


> References (within the C99 spec): 
>   §6.7.3 ‘Type qualifiers’, para [#4]
>   §6.7.5.1 ‘Pointer declarators’ para [#1]
> 
> I haven't checked whether C89 differs from C99 in any of these points,
> though I'm not aware of any differences [other than existence of `restrict'].

I've got much more experience with C89 than C99.  I don't remember any older,
different behaviour of the const keyword, so I'd say it's the same in C89.

BTW: Ralph, if you're looking for a C reference K&R's C is on my bookshelf
above my desk, you may borrow it.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 489 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/developers/attachments/20100322/f51f778e/attachment.sig>


More information about the developers mailing list