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:
- Create a variable that will hold the memory location of the first element of an array (the array
newHeaderMsg.headerText[]
). The variable is calledstrstart
. - Set the memory location of
strstart
equal to 'D' - Get the current system time, and pass its value to the variable
timeWarningStarted
. - If a particular selection is made on the GUI (i.e.
warningSelect
is set to1
, begin a timer, and set thewarningTimeout
variable to 15 (15 seconds). Then get the current system time, and set its value to the variabletimeWarningDisplayed
. - 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'. - Check whether the
timeWarningDisplayed
variable is greater than or equal to the sum oftimeWarningStarted
andwarningTimeout
(if it is, then the warning has been displayed for the intended length of time) - If
timeWarningDisplayed
is greater than or equal to the sum oftimeWarningStarted
andwarningTimeout
, then set the value of*strstart
to the value oforiginalStrStartValue
, i.e. set it back to 'D'. - Otherwise, if
warningSelect
is something other than '1', set the value of*strstart
to the value oforiginalStrStartValue
, 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?