www.digitalmars.com         C & C++   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?

#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