Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!snorkelwacker!mit-eddie!mit-amt!peter From: peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) Newsgroups: comp.lang.c++ Subject: Re: Constant Question Keywords: const Message-ID: <1405@mit-amt.MEDIA.MIT.EDU> Date: 16 Jan 90 01:05:37 GMT References: <130286@sun.Eng.Sun.COM> Reply-To: peter@media-lab.media.mit.edu (Peter Schroeder) Organization: MIT Media Lab, Cambridge MA Lines: 30 In article <130286@sun.Eng.Sun.COM> rbogen%dreams@Sun.COM (Richard Bogen) writes: >The definition of a constant is an identifier whose >associated value cannot be modified (even through pointers). >Yet when the following program is run the value printed is 0.0. >Can anyone give me a "pointer" to what is happening here? > >main() >{ > const float PI = 3.14159; > float *const ptr = &PI; > *ptr = 0.0; > printf("%PI = %f\n",PI); >} What you declared is not a pointer to a const, but a const pointer. Hence you can not assign to the this const pointer another address, but you can modify what it points to. What you want is: main() { const float PI = 3.14159; const float *ptr = &PI; *ptr = 0.0; // error printf("%PI = %f\n",PI); } Peter peter@media-lab.media.mit.edu