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
write a c program thal will find all sequences of length N that produce the sum is Zero, print all possible solutions?
What are the three constants used in c?
How many types of operator or there in c?
Please send me WIPRO technical question to my mail ID.. its nisha_g28@yahoo.com please its urgent
How do you do dynamic memory allocation in C applications?
What are the similarities between c and c++?
How #define works?
What are the types of variables in c?
How can you call a function, given its name as a string?
‘SAVEPOINT’ and ‘ROLLBACK’ is used in oracle database to secure the data comment. Give suitable examples of each with sql command.
What is pass by value in c?
Can a pointer point to null?
How can I pad a string to a known length?
Write a program with dynamically allocation of variable.
a sequence of bytes with one to one corrspondence to those in the external device a) sequential addressing b) address c) byte code d) none