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.
Related
This question already has answers here:
declaring a variable-length array as a global variable in C
(4 answers)
Closed 2 years ago.
I'm trying to understand the difference that the scoping makes:
//global scope
int size = 4;
int array[size]; // error: variably modified 'array' at file scope
int main(void) {
int buff[size]; // works!
}
how does using a variable as array size doesn't work globally but works inside main? It would work if I use a macro instead.
Also, does using const matter for size?
simply make it compile time integral constant expression, since array length must be specified at the compile time, with define
#define SIZE 6
int array[SIZE];
This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 2 years ago.
I saw the piece of the code. But do not know what the purpose is.
void a()
{
static const char *string = "STRING";
...
(void)string; // <---- What the purpose of the line is?
...
}
(void) before a variable like that creates an empty expression and is used to silence warnings about a variable not being used by the program.
In this specific case it would be better to simply comment out the variable declaration.
This question already has answers here:
variably modified array at file scope in C
(4 answers)
"static const" vs "#define" vs "enum"
(17 answers)
Defining the size of an array using a const int
(3 answers)
Closed 3 years ago.
int const a=9;
int stack[a];
int main()
{
return 0;
}
The above code gives an error:variably modified 'stack' at file scope
But when I change the code to :
#define b 3
int stack[b];
int main()
{
return 0;
}
It compiles without error.While both #define and const variable are used for defining the constant identifier then why there is error when I use the const var instead of #define.I searched the similar question but all they gave solution about the error but no reason to it.
Searched about the const and #define and found that sometimes gcc compiler recognizes const as readonly but it is too confusing.
In C language static storage variables can have size defined by the constant expression. Using the variable (even the constant one) as the size is not such a expression.
The error is 100 percent correct.
The second case: proprocessor replaces textually the b with 3 which is the constant expression
Constant expression is something which is evaluated during the compilation. Variable value can be only evaluated runtime.
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;
^
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.