solved: understand compiler warning and note?

In my sketch for a Mega2560 the compiler sends a warning and a note which I don’t understand. The sketch can be uploaded und runs but I’m worried about the warning and note.

byte rfcnt=0;  // globally declared
...
rfcnt=byte(atoi(sm_cmd+3));
      switch (rfcnt) {
      case 1: // HTTP_Fehler im EEPROM listen und loeschen
        list_HTTP_errors();
        EEPROM.update(EEPROM_HTTP, 0);
        break;
      case 2: // HTTP-Fehler auf SD-Karte listen (loeschen mit ch)
        uint16_t si=1;      // <------------ here a note
        if ((hvar=SD_L1_ReadBlock(startsec_out, talk_buff))==0) {
          while (hvar==0 && talk_buff[0]==42 && talk_buff[1]==35) { // *#
            Serial.print((char*)talk_buff);
            hvar=SD_L1_ReadBlock(startsec_out+si++, talk_buff);
          }
          if (hvar==1) {
            Serial.print(F("Lesefehler bei Block "));
            Serial.println(startsec_out+si-1);
          }
        }
        else {
          Serial.print(F("Lesefehler bei Block "));
          Serial.println(startsec_out);
        }
        break;
      case 3:      // <------------ here a warning
        for (hvar=0; hvar<=MAX_RFMISS_CNT; hvar++) {
          if (nowdat_hist[hvar] > 0) {
            Serial.print(hvar);Serial.print(" ");
            Serial.println(nowdat_hist[hvar]);
          }
          nowdat_hist[hvar]=0;   // zuruecksetzen
        }
      } // switch

The line "case 3: " generates a warning:

…\VoiceSMSClientDisplay.ino:1473:12: warning: jump to case label [-fpermissive]

case 3: // <------------ here a warning

^

The line “uint16_t si=1;” generates a note:

…\VoiceSMSClientDisplay.ino:1457:18: note: crosses initialization of ‘uint16_t si’

uint16_t si=1;

^~

Explanations for these compiler messages would have a calming effect on my mood :confused:

(Arduino IDE 1.8.12 on Win10, 32 bit)

next time post your entire code

"si" needs to be declared outside the switch statement.

         uint16_t si=1;

i don't know how particular compilers implement switch statements and therefore don't know the scope of variable defined within them. It's not obvious that each case in a switch statement is a coding block (i.e. code w/in braces having it's own scope)

If you declare a variable in a case, wrap the case in { } so that its scope is local to that case only.

If a variable is to be used in more than one case, declare it before the switch.

Otherwise you can arrive at code involving a variable that hasn't been declared/initialized, which gives
undefined behaviour.

Thank you, the braces solved both compiler messages :slight_smile: