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 ?    16 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 ?    1 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

Which of the following is not an infinite loop ? a.while(1){ .... } b.for(;;){ ... } c.x=0; do{ /*x unaltered within theloop*/ ... }while(x==0); d.# define TRUE 0 ... while(TRUE){ .... }

7 Answers   TCS,


difference between c and c++?

2 Answers  


if a five digit number is input through the keyboard, write a program to calculate the sum of its digits. (hint:-use the modulus operator.'%')

23 Answers  


Which of the following is not a valid declaration for main ()? 1) int main() 2) int main(int argc, char *argv[]) 3) They both work

2 Answers  


sum of two integers values only other then integer it should print invalid input.

1 Answers  






to write a program, that finds the minimum total number of shelves, including the initial one, required for this loading process. The packets are named A, B, C, D, E &#133;&#133;.. Any numbers of packets with these names could be kept in the shelf, as in this example: [ZZLLAAJKRDFDDUUGGYFYYKK]. All packets are to be loaded on cars. The cars are lined in order, so that the packets could be loaded on them. The cars are also named [A, B, C, D, E,&#133;&#133;&#133;&#133;.].

2 Answers   Infosys, TCS,


write a program to add two numbers of any size.....(remember any size)

1 Answers  


Name the language in which the compiler of "c" in written?

3 Answers   Bajaj,


To find whether a number is even or odd without using any conditional operator??

12 Answers   IBM, College School Exams Tests,


what is bitwise operator?

1 Answers   IBM,


Go through this linked list concept.While traversing through the singly linked list sometimes the following code snippet "while(head != NULL)" is used and other times "while(head->link != NULL)"is used(Here head is the pointer pointing to the first node,node has two parts data part and link part).What is the difference between head != NULL and Head->link != NULL and in which situation are they used?

1 Answers   Oracle,


Write a program to find the smallest and largest element in a given array in c language

11 Answers   Microsoft, Vembu,






Categories