-2

I have the following function in my C++, which I am tryig to use to update the information displayed on part of a GUI:

void store::updateHeader(){
    ...
    strstart = &newHeaderMsg.headerText[0];
    *strstart = 'D';
    const char* originalStrStartValue = strstart;
    double timeWarningStarted = SimulationTime::Instance()->getSimulationTime();

    if(warningSelect == 1){
        timer->begin();
        ...
        warningTimeout = 15; // time in seconds
        double timeWarningDisplayed = SimulationTime::Instance()->getSimulationTime();

        if(timerStarted){
            *strstart = 'R';
            if(timeWarningDisplayed >= (timeWarningStarted + warningTimeout)){
                *strstart = *originalStrStartValue;
            }
        } else {
            *strstart = originalStrStartValue;
        }
    } else {
    *strstart = originalStrStartValue;
    }
}

Basically, the logic of the function is:

  1. Create a variable that will hold the memory location of the first element of an array (the array newHeaderMsg.headerText[]). The variable is called strstart.
  2. Set the memory location of strstart equal to 'D'
  3. Get the current system time, and pass its value to the variable timeWarningStarted.
  4. If a particular selection is made on the GUI (i.e. warningSelect is set to 1, begin a timer, and set the warningTimeout variable to 15 (15 seconds). Then get the current system time, and set its value to the variable timeWarningDisplayed.
  5. If the timer has started, set variable at the memory location of the first element in the array (i.e. the memory location of strstart) to 'R'.
  6. Check whether the timeWarningDisplayed variable is greater than or equal to the sum of timeWarningStarted and warningTimeout (if it is, then the warning has been displayed for the intended length of time)
  7. If timeWarningDisplayed is greater than or equal to the sum of timeWarningStarted and warningTimeout, then set the value of *strstart to the value of originalStrStartValue, i.e. set it back to 'D'.
  8. Otherwise, if warningSelect is something other than '1', set the value of *strstart to the value of originalStrStartValue, i.e. set it back to 'D'.

The issue that I'm having is trying to set the value of the variable originalStrStartValue. If I run the program with the code as it is above, then despite having declared originalStrStartValue with const, because I chage the value of the variable it is pointing to, its value also changes.

If I try to set it to the memory location of strstart instead, i.e.

const char* originalStrStartValue = *strstart;

then I get the compile error:

Error: a value of type "char" cannot be used to initialize an entity of type "const char * "

So my question is, how can I create a 'default' value for a variable at a particular memory location in a function, then in the same function, change the value of that variable when a particular condition is true, but automatically revert back to the default value the moment that condition is no longer true?

Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

1 Answers1

3

The error you get is expected and clear. You cannot convert char to char pointer as those two types are completely unrelated.

As I understand, you only want to change first character of character array. Why don't you declare originalStrStartValue as char (or const char) and then revert to that value if needed?

    const char originalStrStartValue = *strStart;
    ...
    *strStart = originalStrStartValue
Jakiša Tomić
  • 290
  • 1
  • 7