switch case surprised me

I had a bit too short char array in a switch case and it messed an another variable. What was surprising was that the one too long case was not supposed to happen during my testing. This demonstrates what I am talking about

char viikonpaiva[6];
byte dayOfWeek2;
byte minute2;
main ()
{
minute2=5;
dayOfWeek2=6;
switch(dayOfWeek2)
{
  case 1:

         strcpy(viikonpaiva, "Sun");
    break;
  case 2:

         strcpy(viikonpaiva, "Maa");
    break;
  case 3:

         strcpy(viikonpaiva, "Tiis");
    break;
  case 4:
 ;
        strcpy(viikonpaiva, "Keskiviikkona");
    break;
  case 5:

         strcpy(viikonpaiva, "Tor");
    break;
  case 6:

       strcpy(viikonpaiva, "Perj");
    break;
  case 7:

      strcpy(viikonpaiva, "Laua");
    break;
  }
 Serial.println(minute2); //minute2 is not 5 anymore
}

strcpy(viikonpaiva, "Keskiviikkona");Count the letters

TolpuddleSartre:
strcpy(viikonpaiva, "Keskiviikkona");Count the letters

Indeed.
I quesstimated a good value but quessed wrong. I still wonder why it matters to a program when that long value is not used.

Here is the real program clip by the way. Lenght of char viikonpaiva was the problem. This is a part of RTC/LCD sketch, viikonpaiva is dayofWeek, keskiviikona is at wednesday and so on.

char viikonpaiva[15];
..
..
if (minute2<10)
  {
    //Serial.print("0");
       sprintf(minuutti, "0%u", minute2);
  }
  else
  {
       sprintf(minuutti, "%u", minute2);   
    }

 
  if (second2<10)
  {
 
       sprintf(sekunti, "0%u", second2);
  }
  else
  {
       sprintf(sekunti, "%u", second2);   
    }
 
 switch(dayOfWeek2){
  case 1:
    //Serial.println("Sunday");
         strcpy(viikonpaiva, "Sunnuntaina");
    break;
  case 2:
    //Serial.println("Monday");
         strcpy(viikonpaiva, "Maanantaina");
    break;
  case 3:
    //Serial.println("Tuesday");
         strcpy(viikonpaiva, "Tiistaina");
    break;
  case 4:
   // Serial.println("Wednesday");
        strcpy(viikonpaiva, "Keskiviikkona");
    break;
  case 5:
    //Serial.println("Thursday");
         strcpy(viikonpaiva, "Torstaina");
    break;
  case 6:
  //  Serial.println("Friday");
       strcpy(viikonpaiva, "Perjantaina");
    break;
  case 7:
 //   Serial.println("Saturday");
      strcpy(viikonpaiva, "Lauantaina");
    break;
  }

 
  sprintf(tunti,"%u", hour2);
  sprintf(paiva,"%u", dayOfMonth2);  
  sprintf(kuukausi,"%u", month2);
  sprintf(vuosi,"%u", year2);    


  strcpy(paivays, viikonpaiva); 
  strcat(paivays," " );
  strcat(paivays, sekunti);
  strcat(paivays,"." );
  strcat(paivays, minuutti);
 
  strcat(paivays,"." );
  strcat(paivays, tunti);
  strcat(paivays," " );
  strcat(paivays, paiva);
  strcat(paivays,"." );
  strcat(paivays, kuukausi);
  strcat(paivays,"." );
  strcat(paivays,"20" );
  strcat(paivays, vuosi);

TolpuddleSartre: strcpy(viikonpaiva, "Keskiviikkona");Count the letters

That code (case 4) is not executed because dayOfWeek2 was set to 6 and hence I wonder how it could have caused the problem.

sterretje: That code (case 4) is not executed because dayOfWeek2 was set to 6 and hence I wonder how it could have caused the problem.

The code in the original post doesn't even compile, so how it behaves is moot.

perse edellä puuhun…

  if (minute2<10)
  {
    //Serial.print("0");
       sprintf(minuutti, "0%u", minute2);
  }
  else
  {
       sprintf(minuutti, "%u", minute2);   
    }

or simply

sprintf(minuutti, "%02u", minute2);

oqibidipo:
perse edellä puuhun…

  if (minute2<10)

{
    //Serial.print(“0”);
      sprintf(minuutti, “0%u”, minute2);
  }
  else
  {
      sprintf(minuutti, “%u”, minute2); 
    }




or simply


sprintf(minuutti, “%02u”, minute2);

Yeah/joo. I did this in the middle of night with absolutely minimum brain function possible. That is, I copied an example. Täytyy koittaa tuota joku päivä/I have try your code.

To sterretje.

sterretje:
That code (case 4) is not executed because dayOfWeek2 was set to 6 and hence I wonder how it could have caused the problem.

Yes, that is the question to which I would like to get an answer…

strncpy() is a better choice.

Why bother trying to figure out why such a serious programming error has some particular, seemingly random consequence?

LMI1: To sterretje.Yes, that is the question to which I would like to get an answer..

Why don't you post the actual code you used and the actual output from it?

LMI1: This demonstrates what I am talking about

char viikonpaiva[6];
byte dayOfWeek2;
byte minute2;
main ()
{
minute2=5;
dayOfWeek2=6;
...
 Serial.println(minute2); //minute2 is not 5 anymore
}

main() needs a return type. You should not call Serial.println() without first calling init() and Serial.begin(baudrate). When I re-write it as an Arduino sketch I don't get an error. This, running on an Arduino UNO, displays:

Perj
6
5
char viikonpaiva[6];
byte dayOfWeek2;
byte minute2;
void setup()
{
  Serial.begin(115200);
  minute2 = 5;
  dayOfWeek2 = 6;
  switch (dayOfWeek2)
  {
    case 1:
      strcpy(viikonpaiva, "Sun");
      break;


    case 2:
      strcpy(viikonpaiva, "Maa");
      break;


    case 3:
      strcpy(viikonpaiva, "Tiis");
      break;


    case 4:
      strcpy(viikonpaiva, "Keskiviikkona");
      break;


    case 5:
      strcpy(viikonpaiva, "Tor");
      break;


    case 6:
      strcpy(viikonpaiva, "Perj");
      break;
    case 7:


      strcpy(viikonpaiva, "Laua");
      break;
  }
  Serial.println(viikonpaiva);
  Serial.println(dayOfWeek2);
  Serial.println(minute2); //minute2 is not 5 anymore
}


void loop() {}

I shouldn't have called my short code as demonstration, it is of course more like pseudo code and will not compile.

If somebody wants I can post the whole long code, it probably behaves differently if I change the code.

LMI1: I shouldn't have called my short code as demonstration, it is of course more like pseudo code and will not compile.

If somebody wants I can post the whole long code, it probably behaves differently if I change the code.

Or you can whittle your code down to something that demonstrates the problem.

In so doing, you'll hopefully find the source of your problem yourself.

Posting code that doesn't demonstrate the problem, and cannot even compile, just wastes time.

I found a way to download the whole problem sketch. TFT_SD_rtc_lyh.ino
There is swith case with char arrays of different sizes. One of the strings is too long and it seems to have an effect (it overwrites a variable) even though that case was not hit during my testing.

Variable declaration
char paivays[40];
char sekunti[4];
char minuutti[4];
char tunti[4];
char paiva[4];
char viikonpaiva[17]; //this must be long enough
char kuukausi[4];
char vuosi[5];

problem code snippet
switch(dayOfWeek2){
  case 1:
    //Serial.println("Sunday");
         strcpy(viikonpaiva, "Sunnuntaina");
    break;
  case 2:
    //Serial.println("Monday");
         strcpy(viikonpaiva, "Maanantaina");
    break;
  case 3:
    //Serial.println("Tuesday");
         strcpy(viikonpaiva, "Tiistaina");
    break;
  case 4:
   // Serial.println("Wednesday");
        strcpy(viikonpaiva, "Keskiviikkona");
    break;
  case 5:
    //Serial.println("Thursday");
         strcpy(viikonpaiva, "Torstaina");
    break;
  case 6:
  //  Serial.println("Friday");
       strcpy(viikonpaiva, "Perjantaina");
    break;
  case 7:
 //   Serial.println("Saturday");
      strcpy(viikonpaiva, "Lauantaina");
    break;
  }

TFT_SD_rtc_lyh.ino (16.5 KB)

Are you ABSOLUTELY CERTAIN that that snippet isn't being called more than once?

I checked the sketch with various lengths of array and did not find the problem I am talking about. I must have had much too short arrays so that even current date did not fit.

I am sorry to have wasted everybodys time.

char viikonpaiva[14]; Tiis 57.52.19 17.7.2018

char viikonpaiva[13]; Tiis 25.54.19 17.7.2018

char viikonpaiva[12]; Tiis 49.55.19 17.7.2018

char viikonpaiva[11]; Tiis 19.57.19 17.7.2018

char viikonpaiva[10]; Tiis 43.58.19 17.7.2018

char viikonpaiva[9]; Tiis 15.00.20 17.7.2018

char viikonpaiva[9]; Tiis 15.00.20 17.7.2018