Is it possible to change the value of a const variable?
My aim is to change the directory of the Shell.
I have set const char* path = "C:\\"
as current directory. So if the user inputs "gd" then it will ask a directory to be set as the new path
. However, I have no idea how can a value of a const variable change.
I found this code(modified) to change the value with user input:
char changeConst(const char &n)
{
char* ptr = const_cast<char*>(&n);
char response;
cout << "Enter new directory: ";
cin >> response;
*ptr = response;//error: unhandled exception!
return n;
}
There is an unhandled exception error at *ptr = response;
.
I called the above changeConst
function in the main as:
else if(response == "gd"){
const char& p = *path;
changeConst(p);
}
Using dirent.h to excess DIR
Using Microsoft Visual Studio Express 2012