How to access or modify the const variable in c ?
Answers were Sorted based on User's Feedback
Answer / gaurav bhandarkar
Author : Gaurav M. Bhandarkar
Yes u can modify constants...
Theory:
const int z = 420; // z is constant
&z (address of constant)
z (value of constant)
imp:
*(int*)((char*)&(*((char*)&z+1))-1) is
a unity/identity pointer operation resulting in z
That is:-
printf("%d | %d",*(int*)((char*)&(*((char*)&z+1))-1),z);
OUTPUT: 420 | 420
code:
const int z = 420;
printf("%d | %d\n",*(int*)((char*)&(*((char*)&z+1))-1),z);
//o-p 420 | 420
*((char *)&z+1) = 21; //corrupting the constant
printf("%d | %d",*(int*)((char*)&(*((char*)&z+1))-1),z);
//o-p 5540 | 420
___
The 2 similar printf's(check they are same)
o/p different values for same "z"
which is a constant!
thus the constant was corrupted!
Is This Answer Correct ? | 53 Yes | 3 No |
Answer / saneesh a t
Actually the keyword const is not for the programmer who work with the normal
general purpose computer. By declaring a variable as const, the compailar shows
warning/error to the modification to the variable. At runtime you can modify the memory location
using any technique, or an external hacking program can change the value of the variable.
Suppose your program is compiled to run with a microcontroller with flash or EEPROM or such
memory. Now your variable with const qualifier will be stored in the FLASH of EEPROM memory,
which cann't be modified, and the technique is used to save the RAM space which is too small in
size for a micro controller. In this case too, a FLASH/EEPROM write can chage the value of the
const varriable.
Is This Answer Correct ? | 17 Yes | 1 No |
Answer / vignesh1988i
const. variable cannot be modified........ if you declare an
variable as:
const char i=90;
throught the program 'i' cant be modified
but #define macro can replace const.. and that macro can be
modified.... but its disadvantage is it will blindly
substitute the data which is #defined.......
Is This Answer Correct ? | 21 Yes | 9 No |
Answer / vijay kumar (mainpuri)
Answer of #6 , output is wrong
correct out is given below :
Value is: 20
Value is: 20
(above output based on turbo c++ IDE)
Hence, const value can't be change.
Is This Answer Correct ? | 15 Yes | 5 No |
Answer / sri harsha s
Its compiler dependant.
Using pointer you can modify the content of a 'const'
variable. It works in gcc.. you will get the following
warning when you compile that program.
warning: initialization discards qualifiers from pointer
target type.
I am not sure about turbo c++.
Is This Answer Correct ? | 9 Yes | 1 No |
Answer / mr.x
int main()
{
const volatile int no=10;
int *ptr;
ptr=(int *)&no;
*ptr=30;
printf("%d %d",no,*ptr);
return 0;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / varunreddy
try this it will work:
#include<stdio.h>
int main()
{
const int i=10;
*(int *)&i=i++;
printf("%d\n",i);
return 0;
}
Is This Answer Correct ? | 3 Yes | 3 No |
Answer / varunreddy
a small modification to answer #13
#include<stdio.h>
int main()
{
const int a=10;
*(int *)&a=25
printf("%d\n",a);
return 0;
}
Is This Answer Correct ? | 3 Yes | 3 No |
Answer / raj
Answer 6 would be true if you change the variable to const
volatile.
Is This Answer Correct ? | 0 Yes | 1 No |
Input is "rama loves rajesh and rajesh Loves rama also and rajesh wear gloves and bloves" To print output is count the numbers of times repeted the word love without case sensitive.
write a program that will open the file, count the number of occurences of each word in the the complete works of shakespeare. You will then tabulate this information in another file.
who developed c and why he developed c?
write a c program for print your name .but,your name may be small letter mean print a capital letter or your name may be capital letter mean print a small letter .example \\enter ur name : sankar The name is: SANKAR (or) enter your name:SAnkar The name is:saNKAR
What is restrict keyword in c?
write a program to convert a expression in polish notation(postfix) to inline(normal) something like make 723+* (2+3) x 7 (not sure) just check out its mainly printing expression in postfix form to infix.
0 Answers Lovely Professional University,
To what value are pointers initialized? 1) NULL 2) Newly allocated memory 3) No action is taken by the compiler to initialize pointers.
a value that does not change during program execution a) variabe b) argument c) parameter d) none
What are the advantages of using new operator as compared to the function malloc ()?
What are pointers? What are different types of pointers?
Write a program for the following series? 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 1234567890987654321 123456789010987654321 12345678901210987654321 1234567890123210987654321 .........1234321............ ..........123454321............ ..........12345654321............ 7 8 9 0 1 Pls............?
Difference between goto, long jmp() and setjmp()?