Saturday, 24 August 2013

const variable value is changed by using a pointer

const variable value is changed by using a pointer

The output of the following program is 50 on gcc. How is it possible as x
is constant variable and *p is x itself as p is a constant pointer
pointing to value at x. Where as turbo c gives compiler error. Is it an
undefined behaviour? please explain.
#include<stdio.h>
void main(){
const int x=25;
int * const p=&x;
*p=2*x;
printf("%d",x);
}

No comments:

Post a Comment