rbogen%dreams@Sun.COM (Richard Bogen) (01/16/90)
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 = Π
*ptr = 0.0;
printf("%PI = %f\n",PI);
}peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (01/16/90)
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
vsh@etnibsd.UUCP (Steve Harris) (01/20/90)
In article <1405@mit-amt.MEDIA.MIT.EDU> peter@media-lab.media.mit.edu (Peter Schroeder) writes: > 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 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Peter corrected the code, but didn't answer the question! Isn't the real problem in line 2, not line 3? Clearly, ptr is a const pointer to a (variable) float. The pointer is constant, but the thing pointed to is modifyable. In line 2, you declare this pointer and initialize it to point to PI. However, PI is a const float, not modifyable. Shouldn't the compiler refuse to allow you to initalize (or assign) a pointer (const or otherwise) to a const float? Shouldn't the compiler insist that the pointer be of type "const float *"? My g++ generates a warning at line 2, and creates a second object for ptr to point to, so PI remains unchanged and *ptr is initalized to 3.14159 but then is changed to 0.0. -- Steve Harris - Eaton Corp. - Beverly, MA - uunet!etnibsd!vsh
peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (01/24/90)
Sender: Reply-To: peter@media-lab.media.mit.edu (Peter Schroeder) Followup-To: Distribution: Organization: MIT Media Lab, Cambridge MA Keywords: const Ok, I'll bite my tongue off... In article <1103@etnibsd.UUCP> vsh@etnibsd.UUCP (Steve Harris) writes: >In article <1405@mit-amt.MEDIA.MIT.EDU> peter@media-lab.media.mit.edu (Peter Schroeder) writes: >>In article <130286@sun.Eng.Sun.COM> rbogen%dreams@Sun.COM (Richard Bogen) writes: main() { const float PI = 3.14159; float *const ptr = &PI; *ptr = 0.0; printf("%PI = %f\n",PI); } [some smart comments of mine] main() { const float PI = 3.14159; const float *ptr = &PI; *ptr = 0.0; // error printf("%PI = %f\n",PI); } >Peter corrected the code, but didn't answer the question! Well, umh, ... So, I just ran this through cfront 2.0 with float *const ptr = &PI; I do not get a complaint and it prints 0.0. Now that is a bug as far as I am concerned. If I write float *ptr = &PI; the compiler complains (rightly so). >My g++ generates a warning at line 2, and creates a second object for ptr >to point to, so PI remains unchanged and *ptr is initalized to 3.14159 but >then is changed to 0.0. I am not sure I would like that, at least not without a warning about using an intermediate. Peter peter@media-lab.media.mit.edu