If my question seems familiar, it's virtually a copy from a post by guy_c a year ago. But his solution doesn't work for me.
My reason for asking is the same; because I want zero warnings.
Can someone please help me understand why does the compiler complain that my variable may be uninitialized? I simplified the function as much as I could before posting. The entire sketch is spread over ten tabs, so I am just posting the simplified function.
The warning is: warning digit1 may be used uninitialized in this function
Yes, by changing int digit1;
to int digit1=0;
, the warning goes away. So my inquiry is academic.
digit1 and digit3 are both local to the function and used alike in the function, but only digit1 gets the warning.
Why?
// ****************** mqtt callback *********************
// This function is executed when some device publishes a message to a topic that this ESP8266 is subscribed to.
//
void callback(String topic, byte * message, unsigned int length) {
int digit1;
int digit3;
int intHour;
char buf [2];
digitalWrite(ledPin, ledON); //Turn on LED and start timing it.
ledMillis = millis();
// Convert the character array to a string
String messageString;
for (uint16_t i = 0; i < length; i++) {
messageString += (char)message[i];
}
messageString.trim();
messageString.toUpperCase(); //Make the string upper-case
Serial.print("messageString: ");
Serial.print(messageString);
Serial.println();
if (topic == timeTopic) {
//Time from Node Red is always 5 characters.
//For example: "12:34" or "01:05"
//Add the decimal point to the second digit since the display doesn't have a colon.
switch (message[1])
{
case 48:
digit1 = num0 + segDP;
break;
case 49:
digit1 = num1 + segDP;
break;
case 50:
digit1 = num2 + segDP;
break;
case 51:
digit1 = num3 + segDP;
break;
case 52:
digit1 = num4 + segDP;
break;
case 53:
digit1 = num5 + segDP;
break;
case 54:
digit1 = num6 + segDP;
break;
case 55:
digit1 = num7 + segDP;
break;
case 56:
digit1 = num8 + segDP;
break;
case 57:
digit1 = num9 + segDP;
break;
}
} //timeTopic
if (topic == trendTopic) {
switch (messageString.toInt())
{
case 1:
digit3 = segA + segJ + segM; //Tall up arrow
break;
case 2:
digit3 = segA + segJ;
break;
case 3:
digit3 = segK;
break;
case 4:
digit3 = segG2;
break;
case 5:
digit3 = segN;
break;
case 6:
digit3 = segM + segD;
break;
case 7:
digit3 = segM + segD + segJ; //Tall down arrow
break;
default:
//Should never get here
digit3 = segA + segB + segG2 + segM; //Question Mark
Serial.print(F("Trend? "));
Serial.print(messageString);
Serial.print(F("digit3= "));
Serial.println(digit3);
} //switch
bgDisplay.writeDigitRaw(3, digit3);
bgDisplay.writeDisplay();
} //if topic==trendtopic
} //callback