480 views
0 votes
0 votes

I went through these two links and found that this behaviour is undefined , but the only issue which I have is that in one sense we say that since local variables live on stack , hence they cannot be located on read only memory region but if this is so then if I try to alter the value of the constant directly through assignment then why does it show error if it is not located in the read only memory region : 
 

 

#include
 

  #include  int main()  {  const int a=12;  int *ptr ;  ptr=&a;  *ptr=8; // no error  a=45; // error  printf("\n %d", a);  return 0;  } 

http://stackoverflow.com/questions/3801557/can-we-change-the-value-of-a-constant-through-pointers

1 Answer

0 votes
0 votes

auto variables are in stack and there is no RO section inside stack, But compiler can do constant propagation and a constant variable might not have a memory. This can throw a compile error or unexpected result.

Go through this Link

http://www.geeksforgeeks.org/const-qualifier-in-c/

Related questions

0 votes
0 votes
1 answer
2
radha gogia asked Jul 28, 2015
958 views
int * p(void) { int *x; *x=10; return (x); } We create *x, and dereference it before assigning anything to it (x). Most likely, when we are declaring something on the sta...
1 votes
1 votes
1 answer
3
Na462 asked May 4, 2018
724 views
Please tell me about all the variations of these above types of questions that can be asked?
2 votes
2 votes
1 answer
4
Hrithik Vashishtha asked Jul 4, 2022
363 views
#include <stdio.h int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i] + 1; i++; } i ; for (j = 7; j 4; j ) { int i = j/2; ...