How to access or modify the const variable in c ?




Answers were Sorted based on User's Feedback



How to access or modify the const variable in c ?..

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 ?    51 Yes 3 No

How to access or modify the const variable in c ?..

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

How to access or modify the const variable in c ?..

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




How to access or modify the const variable in c ?..

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

How to access or modify the const variable in c ?..

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

How to access or modify the const variable in c ?..

Answer / guest

use pointers and u can do anything...

Is This Answer Correct ?    23 Yes 17 No

How to access or modify the const variable in c ?..

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

How to access or modify the const variable in c ?..

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 ?    2 Yes 2 No

How to access or modify the const variable in c ?..

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

How to access or modify the const variable in c ?..

Answer / raj

Answer 6 would be true if you change the variable to const
volatile.

Is This Answer Correct ?    0 Yes 1 No

Post New Answer



More C Interview Questions

what are the stages of compilation

1 Answers   Bosch,


main() { struct test { char c; int i; char m; } t1; printf("%d %d\n", sizeof(t1), sizeof(t1.c)); }

1 Answers   Vector, Vector India,


player is good if the following conditions are satisfied: He is either from “India”, “Japan” or “Korea” He has a minimum of 3 years of experience He has won at least 3 major tournaments, and one of them must be "World Championship Tournament". He must know more than 10 techniques. (but see next question) If he knows less than 10 techniques, then he must be a world champion. His name contains at least 3 words. For example "Sachin Tendulkar" will not meet this condition. Write a method: boolean isGoodPlayer(String name, String country, int yearsExperience, String[] majorTournamentsWon, String[] techniques, boolean isWorldChampion) name country yearsExperience majorTournamentsWon techniques isWorldChampion

1 Answers  


4) Write a program that takes a 5 digit number and calculates 2 power that number and prints it. i have done maximum par but i m findind problem in the commented area. please help...

3 Answers  


Give the logic for this #include<stdio.h> #include<conio.h> void main() { clrscr(); int a=10,b; b=++a + ++a; printf("%d", b); getch(); } Output: 24......How?

2 Answers   Accenture,






20. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Answer:??????

2 Answers  


can we define a function in structure?

2 Answers  


develop algorithms to add polynomials (i) in one variable

0 Answers   TCS, Ignou,


write a program in reverse the string without using pointer,array,global variable declaration,lib fun only using a function?

5 Answers   HCL,


how can you print&scan anything using just one character? :) HINT: printf,scanf similer

2 Answers  


What is false about the following A compound statement is a.A set of simple statments b.Demarcated on either side by curly brackets c.Can be used in place of simple statement d.A C function is not a compound statement.

5 Answers   TCS, CCEM,


Write a C program to perform some of the operation which can be performed using Single linked list

1 Answers   Qualcomm,






Categories