Teaching const

Scott Daniels daniels at teklds.TEK.COM
Wed Apr 6 06:33:00 AEST 1988


In article <10203 at steinmetz.steinmetz.ge.com> davidsen at kbsvax.steinmetz.UUCP (William E. Davidsen Jr) writes:
>Could someone give me a good method to use when teaching students C,
>such that they will be able to remember the syntax of
>	pointer to {type} const
>	- vs -
>	const pointer to {type}

How about:

const int *****p;
Had better be a pointer to a constant thing (there is no way to identify the
pointer that you are talking about):	pointer to ... pointer to {type} const

"const int i;"  and  "int const j;" both MUST be talking about constant ints.
We can therefore infer that "const int *p;"  and  "int const *j;" both are also 
talking about constant ints (since placing "*ptr" where a variable occurs in a
declaration is how you build declarations of pointers).  Any other meaning 
assigned to "int const *p;" either violates the "throw in a *ptr" rule, or 
yields to the "reductio ad absurdum" above.

Finally, it seems there is only one place left to put the const when talking
about a constant pointer (except following the variable name, but that is 
really non-C-like), so all we have left is "int * const ptr;".

Hope this helps,
-Scott Daniels	daniels at teklds.UUCP



More information about the Comp.lang.c mailing list