Amazing !!!!!!
-
Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik
-
Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik
I suspect that when printf() was evaluating the variables, it knew that 'ch' was a const, thus could only have its original value (i.e., why bother re-evaluating something that is known not to change). Step through this code with the debugger and watch the changes each statement makes.
-
Look at following C statements: const char ch = 'c'; char* pc = (char*)&ch; *pc = 'd'; printf("%c[%u]\n%c[%u]",ch,&ch,*pc,pc); Output of this code is as follows: c[6684148] d[6684148] What I want to know is if both addresses are same then how the value can be different? Thanking You... C.R.Naik
Chintan wrote: Amazing !!!!!! Not really. The compiler treats constant variables very similarly to #defines. It replaces the value of ch with 'c', which is what it is initialised with, simply because it is constant. The compiler does not care what sort of pointer trickery you perform. It assumes that a constant variable is constant, which it should be. Ryan Being little and getting pushed around by big guys all my life I guess I compensate by pushing electrons and holes around. What a bully I am, but I do enjoy making subatomic particles hop at my bidding - Roger Wright (2nd April 2003, The Lounge)
Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late - John Nichol "Point Of Impact"