Constant Variable Value

How will you change the value of a constant variable in C?

Questions by Vinod G S

Showing Answers 1 - 36 of 36 Answers

The only well-defined way to change the value of a const-qualified variable is to also declare it volatile and let some external system change the value. The whole point behind declaring a variable as "const" is that you explicitly do not want the value to be changed by the code.

Tricks like

const int x = 5;
int *y = (int *) &x;
*y = 10;

Invoke undefined behavior; any result is possible. 
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. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.

  Was this answer useful?  Yes

Praneeth

  • Aug 25th, 2011
 

No the constant can be changed by using the pointer. Initialize a pointer to point to the value of a and then change the value using the pointer. It works.....
Try this:

Code
  1. #include <stdio.h>

  2. int main()

  3. {

  4.     const int a = 5;

  5.     printf("%d",a);

  6.     int *k = (int *)&a;

  7.     *k = 10;

  8.     printf("%d",*k);

  9.     printf("%d",a);

  10.     return 0;

  11. }

Praneeth

  • Aug 25th, 2011
 

It works as said by the first two members. Try this :

Code
  1. #include <stdio.h>

  2.  

  3. int main()

  4. {

  5.     const int a = 5;

  6.     printf("%d",a);

  7.     int *k = (int *)&a;

  8.     *k = 10;

  9.     printf("%d",*k);

  10.     printf("%d",a);

  11.     return 0;

  12. }

  13.  

  Was this answer useful?  Yes

Arup

  • Aug 27th, 2011
 

@Praneeth,
how the line
int*k=(int *)&a;
executes....can u explain me...it'll be helpful for me??

  Was this answer useful?  Yes

@Praneeth: unfortunately, that code invokes undefined behavior, as per the following section from the C language standard:

6.7.3 Type Qualifiers
...
5 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. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.115)

Furthermore, a const-qualified object may be placed in a read-only data segment, as per footnote 114:

114) The implementation may place a const object that is not volatile in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.

Your code may generate the result you expect on your system, but that's a matter of luck, not design. You can't guarantee that you'll get the same result on a different system.

  Was this answer useful?  Yes

Sudhir Venugopal

  • Aug 31st, 2011
 

Thought so! You can change the memory value but you can not add the amount to a const variable

Code
  1. #include <stdio.h>

  2.  

  3. Main{

  4. const my_const = 1;

  5. printf("%d

  6. ", my_const);

  7. /*the constant has 1 as value; the value can not be changed, but the const can be traversed through various memory addresses to confuse the reader, programmer*/

  8. my_const *int = (int *) my_const;

  9. my_const++;

  10. printf("%d

  11. ", my_const);

  12. }


The first const value is 1;

The second const value is a memory address; Thanks


  Was this answer useful?  Yes

sapan

  • Sep 8th, 2011
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3.  

  4. int main()

  5. {

  6. const int a=10;

  7. int *p=&a;

  8. printf("before modification a=%d

  9. ",a);

  10. *p=12;

  11. printf("after modification a=%d",a);

  12. getch();

  13.  

  14.   return 0;

  15. }

  16.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions