Warning digit1 may be used uninitialized in this function

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

I assume that you use digit1 somewhere. The compiler can't guarantee that message[1] was one of the characters you check for, so it may take whatever was on the stack.

The difference is the default case that makes sure, that digit3 always gets a value,
that is not the case for the first switch, that sets digit1.

So the compiler sees a flow of execution, that leaves digit1 uninitialized when used.

If a variable declared in a function (local variable) has no initializer it will have the value of what ever is in the memory location that the variable is assigned. It is called an uninitialized local variable and the compiler is just warning you that the variable can contain any value. Global variables are set to 0 by the compiler, but not local variables.

Maybe you should add a non-static to the local part, static locals are initialized and local.

Noted, thank you, @Whandall.

This makes sense, but why don't I get the same warning for digit3?

Don't know. And I can't test compile the code here to investigate.

The switch-case statement for digit3 has a default clause.

That's a red herring - it's about execution paths. There is none that leaves digit3 unset before use because of the default case in the switch.

You don't read answers? I should note that, and not participate in threads of yours.

Sorry- I don't know how I missed that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.