How to access or modify the const variable in c ?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a method in c?

612


What is the basic structure of c?

547


What is malloc and calloc?

560


How can I ensure that integer arithmetic doesnt overflow?

596


What is the purpose of clrscr () printf () and getch ()?

587






Why do we use c for the speed of light?

597


What is a floating point in c?

590


Where are local variables stored in c?

560


What is %d called in c?

748


What is the use of bit field?

625


What is the process to create increment and decrement stamen in c?

579


#define f(g,h) g##h main O int i=0 int var=100 ; print f ("%d"f(var,10));} wat would be the output??

1531


What are structures and unions? State differencves between them.

603


Write a program to print factorial of given number using recursion?

592


Is c still used?

590