By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,964 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,964 IT Pros & Developers. It's quick & easy.

const is puzzling ???

P: 1
Hi

main()
{
const int m=5;
int *p=(int*)&m;
*p=7;
printf("%d",m);
}

why the value of m changed though it is declared as const.
const are stored in Read-Only Memory? How come the value is changed.
in a system without OS , can changing value of const possible ?
Jun 13 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
That's because you are using C. const is weak in C.

Compile this as C++ code and m is still 5.
Jun 13 '07 #2

Expert 10K+
P: 11,448
That's because you are using C. const is weak in C.

Compile this as C++ code and m is still 5.
Const isn't weak in C, it's just that the entire language is an animal ;-)

kind regards,

Jos
Jun 13 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.