![]() |
Please explain the Output ..
Hi all,
I am executing the following code. Code:
int main()Please provide some explanation. Sarma. |
Re: Please explain the Output ..
It's undefined behaviour because you're modifying a constant.
It's up to the compiler of course, but one way to implement consts is as a typed #define and wherever it sees "i" referenced it just replaces it with a typecheck and the value 10 (and it could also exist in memory in case anyone wants to take its address). And you're not going to fool a compiler with *(&i) - it knows these are inverse operations of each other so this is directly equivalent to i, which is 10 (because i is a const). Looking at the generated assembly answers all. This is from Visual Studio 2005: Code:
printf("%d %d\n",*(&i),*j); |
Re: Please explain the Output ..
Remember One thing...
If you cast away the constness of an object that has been explicitly declared as const, and attempt to modify it, the results are undefined, But compiler will allow to compile. for more detail you can refer ... http://publib.boulder.ibm.com/infoce...const_cast.htm |
| All times are GMT +5.5. The time now is 03:04. |