Write to One Element in Arry

At the start of my Code I created an Char Array, but if I get New Data over Serial (in the Final from a Python Script) I want to replace the Elements with the new Words. The Problem I run into is for example, that the second Word I send isn't stored in the second Element of my Array but in the first AND second Element. What is my Mistake?

char *videotitel[28] = {"keine daten ", "keine daten ", "keine daten ", "keine daten ", "keine daten ", "keine daten ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten  ",     "keine daten  ", "keine daten  ", "keine daten ", "keine daten ", "keine daten ", "keine daten ", "keine daten ", "keine daten  ", "keine daten  ", "keine daten  ", "keine daten ", "keine daten ", "keine daten  ", "keine daten "};
while (Serial.available() > 0) {
    static char nachricht[max_message_length];
    static unsigned int message_pos = 0;    
    char inByte = Serial.read();  
    if ( inByte != '\n' && (message_pos < max_message_length - 1) ){
      //Add the incoming byte to our message
      nachricht[message_pos] = inByte;
      message_pos++;
    }
    //Full message received
    else {
      //Add null character to string
      nachricht[message_pos] = '\0';
      //Print the message 
      Serial.println(nachricht);
      Serial.println(zaehleerseriellesarray);      
      videotitel[zaehleerseriellesarray] = nachricht;  
      zaehleerseriellesarray++;
      message_pos = 0;
    }
  }

Arrays can't be assigned this way:

try

strcpy(videotitel[zaehleerseriellesarray] , nachricht);

This is not going to end well.

videotitel is an array of pointers to string literals. Writing to any of them will result in undefined behaviour. E.g.,

strcpy(videotitel[0] , "Uhoh");
Serial.println(videotitel[1]);  // Prints "Uhoh daten".

Using a two dimensional array should fix this issue:

char videotitel[][28] { ... };

// ...

strcpy(videotitel[0] , "Uhoh");
Serial.println(videotitel[0]);  // Prints "Uhoh".
Serial.println(videotitel[1]);  // Prints "keine daten".
1 Like

Yes
exactly

They absolutely CAN be assigned that way. videotitel is an array of "pointer to char", and those pointers CAN be re-assigned, though NOT by writing a new pointer to a string local variable that is about to go out of scope!

That is dangerous. Those strings are all different lengths. What happens if the string you are copting to one of them is longer than the initialization string? Answer: You corrupt memory, and the program crashes.

You defined your array by wrong way:

char *videotitel[28] = {"keine daten ", "keine daten "....

as mentioned @jfjlaros , it is not an array of editable strings, but an array of pointers to string literals. Attempt to edit a literal will lead to "undefined behaviour".

You need to define it as two-dimension array, as example:

char videotitel[28][20] = {"keine daten ", "keine daten "....

where the second index (20) is a maximum length of any single string in the array.

After that you can be able to copy a new data to this strings via strcpy() as I said above:

strcpy(videotitel[zaehleerseriellesarray] , nachricht);
1 Like

Yes, it can. But this assignment will be perfectly useless for OP's task.

Thank you so much, that solved it for me!

You still MUST be sure you do not write past the end of the allocated array!

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