Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - Odd behavior in toy program
Hello group, I just downloaded the Digital Mars C/C++ compiler and was playing around with the piece of code shown at the end of this post. When I compile and run said code, I get the following output: dog = 8, dog_p points to 7 However, when I uncomment the printf("hello world") line I get the output I expect: dog = 7, dog_p points to 7 I'm confused as to what is going on. Anyone care to enlighten me? Thanks! Vic ------------------------------------------------------- #include <stdio.h> main() { const int dog = 8; int *dog_p = (int *)&dog; *dog_p = 7; //printf("hello world!\n"); printf("dog = %d, dog_p points to %d\n", dog, *dog_p); } ------------------------------------------------------- Aug 24 2005
const int dog = 8; Aug 25 2005
In article <dekb43$19lu$1 digitaldaemon.com>, tjulian says...const int dog = 8; Aug 25 2005
"Victor Lam" <Victor_member pathlink.com> wrote in message news:deksaf$2601$1 digitaldaemon.com...here is the code again. of course this code is not meant to do anything useful, which is why i have the int being const, and a non-const int * pointing to the int. it was meant for experimental purposes. #include <stdio.h> main() { const int dog = 8; int *dog_p = (int *)&dog; *dog_p = 7; //printf("hello world!\n"); printf("dog = %d, dog_p points to %d\n", dog, *dog_p); } dog_p, along with the cast, is used to circumvent the const-ness of dog, which is why i expect to see the printf show that the value of dog is 7. in any case, i am able to change the value of dog from 8 to 7 in this manner, but only after i uncomment printf("hello world") which doesn't seem like it should affect the value of dog. that is the question i'm trying to answer here. Aug 25 2005
|