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 does %p mean?
Why is structure padding done in c?
Explain how can a program be made to print the line number where an error occurs?
how to introdu5ce my self in serco
#include main() { enum _tag{ left=10, right, front=100, back}; printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back); }
What is the full form of getch?
What is a union?
What is a nested formula?
How can I run c program?
What is an lvalue in c?
Can you please explain the difference between syntax vs logical error?
Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 10 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.
Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?
Explain what is #line used for?
Can 'this' pointer by used in the constructor?