pradeep
BAN USER- 0of 0 votes
Answers#include <iostream> #include <stdio.h> using namespace std; class A{ public: void fun(int& temp){cout<<"am inside fun()"<<endl; temp = 2000; printf("in fun() temp[%p] temp val[%d]\n",&temp, temp); } }; const int temp=100; int main() { A a; printf("in main() temp[%p] temp val[%d]\n",&temp, temp); a.fun(const_cast<int&>(temp)); cout<<"temp:"<<temp<<endl; }
Questions:
- pradeep in India
1> when i run this pgm @ line "temp=2000;" pgm will crash. what is the reason for sigsegv in this case?
2> if i make const int temp=100; declaration inside main() then, no crash and value change (i.e. temp=2000;) persists only inside fun(). But in the scope of main() this changed value is not seen. whats happening here? But address of temp in both the cases shows the same!! how is this possible??| Report Duplicate | Flag | PURGE
Wipro Technologies Software Analyst C++