Changing value of const int using using pointer [duplicate] - c

This question already has answers here:
Const variable changed with pointer in C
(5 answers)
Closed 9 years ago.
I wrote a C program in gcc and it's giving me very unexpected output. The code is :
#include<stdio.h>
int main(){
const int x=10;
int *p=&x;
*p=11;
printf("%d\n",*p);
printf("%d",x);
printf("\n%u\n",p);
printf("%u", &x);
}
Here output is:
11
10
37814068
37814068
Why do p and &x give the same address(37814068) but different values(5,10)??

Modifying a const variable (directly or through a pointer) invokes undefined behavior. You may not get the same result in another machine.

In the C Standard mofiying a constant is an undefined behaviour. It means that anything could happen it depends on the machine you're running and the compiler your using. In some cases constants are puted on read-only memory and modifying it's value will cause the program to crash.
Exemple of the error generated by the GCC compiler:
error: assignment of read-only location '* p'
*p = 11;
^

Related

returning an address of a local variable [duplicate]

This question already has answers here:
The compiler gives no warning on returning local pointer
(2 answers)
Closed 2 years ago.
'''
int* fun(){
int x=5;
int* p=&x;
return p;//problematic part
}
int main(){
int *c=p; //error when int "*c=&x;"
*c=13;
}
'''
when I create a "integer pointer" in the main function and then assign the address of a local variable to that " integer pointer" through the pointer p , and assign a new value to that particular address, it doesn't give any error or warning ,which I think it should. because when I tried to return the address of "int x" through "&x" and then try to change the value of it, program crashed as I expected.
why does the program not crash any error when I use "pointer p"?????
edit: program should have crashed that's my question not that the reason why compiler gave warnings
The compiler will warn you if you try to return the address directly with return &x;
The reason it does not warn you here is because the compiler "doesn't know" that you have assigned the address of a local variable. The compiler is simply not smart enough.

How this const value is getting changed..? [duplicate]

This question already has answers here:
Why can I change a local const variable through pointer casts but not a global one in C?
(7 answers)
Can we change the value of an object defined with const through pointers?
(11 answers)
In C, can a const variable be modified via a pointer?
(4 answers)
Closed 3 years ago.
I read that constant values cant be changed but in this code below the value of i is getting changed through the use of a pointer. May I know how?
#include <stdio.h>
int main()
{
const int i = 10;
int *ptr = &i;
*ptr = 20;
printf("%d\n", i);
return 0;
}
The output of this code is 20 with a compiler warning.
The behaviour of your code is undefined - the language does not define the behaviour of modifying an object originally declared as const via a pointer that's had const stripped from it.
On some compilers with optimisations turned on, getting 10 as the output is reasonable.

Constant int incement [duplicate]

This question already has answers here:
warning: assignment discards qualifiers from pointer target type
(2 answers)
Closed 6 years ago.
Consider below two cases.
case1: compiler error --> error: increment of read-only variable ‘x’
#include<stdio.h>
main()
{
const int x=5;
printf("%d",++x);
}
case2: Ran successfully with output 6. why?
#include<stdio.h>
main()
{
const int x=5;
int *ptr=&x;
++(*ptr);
printf("%d",x);
}
int *ptr=&x; is a constraint violation, i.e. an "error", invalid code. C language does not support implicit conversion of const int * type to int * type. Your compiler surely issued a diagnostic message for this.
After that diagnostic message your program's behavior is no longer defined by C language, assuming your compiler somehow agreed to compile it. Many C compilers can be configured to refuse to compile this code, even if they accept it in default mode.
You could force the conversion by using a cast
int *ptr = (int *) &x;
This would get rid of the above constraint violation. The compilers would now have to accept your code (with some caveats). But after that modification, the attempt to do ++*ptr would trigger undefined behavior, because it is an attempt to modify a const object.

Modification of const int through a non-const pointer [duplicate]

This question already has answers here:
can casting away constness lead to undefined behavior? [duplicate]
(5 answers)
Closed 7 years ago.
#include <stdio.h>
int main(){
const int a = 10;
*(int*)(&a) = 9; // modify a
printf("%d", a);
return 0;
}
When I run this code on Xcode, the output is 10 (not changed)
When I run this code on Visual Studio Community, the output is 9 (changed)
Why?
This program would compile but exhibits undefined behavior and may output 9 or 10 or something else or may crash who knows.
When you say a is const, you promise that you won't try change the value of a directly or indirectly and compiler may make certain assumptions. If you break the promise unexpected things may happen.
Q: why?
Ans: undefined behaviour.
To explain, if you try to modify a const variable value by accessing is through some non-const pointer, it invokes undefined behaviour.
As per C11 standard, chapter 6.7.3, paragraph 6.
If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.
Note: The recommended signature of main() is int main(void).
const keyword is used not to change the value of variable. If its done forcefully results may be unexpected

Explanation required about how const modifies the behavior of storage [duplicate]

This question already has answers here:
Deep Analysis of Const Qualifier in C
(9 answers)
Closed 8 years ago.
http://www.ideone.com/kRaMj
I found this in the algogeeks forum. Can anyone explain how value of i is still 0, but *p shows 2 although their addresses are the same.
#include<stdio.h>
int main()
{
const int i = 0;
int * p ;
p = (int *) & i;
*p = 2;
printf("(i,p): %x %x \n",&i,p);
printf("(i,p): %d %d \n",i,*p);
}
The output of the program is:
(i,p): bfdf6234 bfdf6234
(i,p): 0 2
That code is causing an Undefined Behavior.
Once you change the value of the constant variable i using a pointer to it p, all bets are off and any behavior might be seen.
Undefined behavior means anything can happen and the behavior cannot be explained as the Standard, which defines the rules of the language does not define any behavior.

Resources