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
What is a structure member in c?
Differentiate between calloc and malloc.
Explain high-order and low-order bytes.
What is 2c dna?
Is javascript based on c?
Explain why c is faster than c++?
explain how do you use macro?
What is the importance of c in your views?
how to find anagram without using string functions using only loops in c programming
What is hungarian notation? Is it worthwhile?
What is a substring in c?
What does *p++ do?
Explain can you assign a different address to an array tag?
What does d mean?
Is int a keyword in c?