Title: Why is it allowed to modify a constant using a pointer in C?
Tags: c;pointers
Question: I was wondering why it is allowed to modify a constant using a pointer?
int main( int argc, char ** argv ) {
const int my_const = 9;
int *ptr = &amp;my_const;
*ptr += 10;
printf("%d", my_const);
return 0;
This prints 19 without any error.
Is this a compiler specific behavior? (I am running gcc 4.7 on Windows)
Thanks...
Comment: Hi M.M, I actually ran the code. There was only a warning to use a constant integer pointer instead of the regular one.

Comment: Hi chux, I was expecting some kind of runtime error since I was modifying a constant.

Comment: Curious, what did you expect the code to be required to do?  Did you expect the code was required to stop and report a  message?

Comment: It's not allowed. Your program breaks the rules of the language. Pay attention to the diagnostic messages emitted by the compiler



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/15/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><pointers>', 'Title': 'Why is it allowed to modify a constant using a pointer in C?', 'ViewCount': 34, 'id': 50340794, 'user_AboutMe': None, 'user_AccountId': 8453234.0, 'user_DisplayName': 'user6341457', 'user_Reputation': 11.0, 'user_Views': 0.0, 'user_id': 6341457.0}}