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

Edited 2 Years Ago by kal_crazy

It is generally not safe to change the value of a constant. The const_cast operator allows you to suspend the protection against doing this when you know it is safe to do so. In some cases, const_cast can be safely used, but not in this case.

The danger with changing the value of a constant is that const tells the compiler that the memory will never be written to, which means that the compiler is allowed to do some optimizations like putting the variable in read-only memory sections, or optimizing away the variable altogether.

The only times that it is safe to use a const_cast is when you know for sure that the variable has not been optimized away or put in read-only memory... and to know that, it takes a bit of insight into the language and compiler mechanics, which is too long to explain here.

So, if you recklessly do a const-cast and write to read-only memory, you will get a crash / error, which is probably what happened to you.

If you want to be able to change the value of path, then don't make path into a const. And you should probably use std::string instead too. As in: std::string path = "C:\\";.

Thanks Mike.

I have removed the changeConst function and added a new global variable std::string currentPath = "C:\\;. This new variable will be used to change the path. However I needed a const char* variable to be used in the opendir(). So I did this: const char* path = currentPath.c_str();
The variable path is used in opendir().

Now the command "gd" is working like it is suppose to. However, whenever I input the directory as "C:\FIFA 13" the currentPath is stored as "C:\FIFA" only.

This is how the currentPath is changed after command "gd" is recognized:

else if(response == "gd"){
                //const char& p = *path;
                //changeConst(p);
                cout << "Enter new directory: ";
                cin >> currentPath;
            }

When you do cin >> currentPath; it will only input the next word, i.e., it stops at the first space character. If you want to input a complete line, you have to do this:

getline(cin, currentPath);

that will read the entire line, including space, up to the new-line character (where the user hits enter).

This question has already been answered. Start a new discussion instead.