0

this is my code. It should be pretty simple, but I don't know what's going wrong. I have two arrays, and fill them with a character 'n'. That's it. It literally just fills the two char arrays with 'n' for every element. For some reason, my output is giving this: screenshot of serial output with wonky shtuffs What am I doing wrong here? Why is it filling with weird stuff, and why is the character array longer then I specified? When I switched the conditions to i < size for the two loops, I got even worse output.image with what I thought was better and origionally did but SHRUG Why is id array 3 instead of just size 1? Same for data array? Do i manually have to enter \0? I didn't think so?

const int idSize = 1;
const int dataSize = 7-idSize-2; //the two is \0 and :, id size is //1, 7 is starting bytes
//[id] [:] [1][2][3] [\0]
// 1   2    3 4   5   6, idk what the 7th byte is, but that is how // many I'm recieving when I print out howMany from wire recieve. //That code isn't included as it isn't important to the issue.
char idMessage[idSize];//1 character
char dataMessage[dataSize];//actual size would be howmany - idsize - 1, the -1 is - ':'. -1 isn't nessisary bc howmany-idsize returns the number, not 0123 but 1234

int id;
double data;
void setup(){
  Wire.begin(9);
  Serial.begin(115200);
}

void loop(){
  for(int i = 0; i <= idSize;  i++){//<= idsize means +1 in size, because starts at 0
    idMessage[i] = 'n';//TODO fill back to front with the message to make sure that there are no extra zeros to wack stuff
// I tried the condition at i<dataSize and i < idSize, but those also didn't work :(
  }
  for(int i = 0; i <= dataSize;  i++){
    dataMessage[i] = 'n';///TODO fill back to front with the message to make sure that there are no extra zeros to wack stuff
  }  

  Serial.print("idMessage ");
  Serial.println(idMessage);
  Serial.print("dataMessage ");
  Serial.println(dataMessage);
}
  • Looks like you’re not null terminating your char arrays – Taekahn May 12 '22 at 01:09
  • 1
    `for(int i = 0; i <= dataSize; i++)` -- A loop that uses `<=` is suspect for an off-by-one error/buffer overrun. `dataMessage[i] = 'n'` -- What happens when `i == dataSize`? – PaulMcKenzie May 12 '22 at 01:14
  • Does this answer your question? ["Junk" displayed when displaying array of characters](https://stackoverflow.com/questions/48683745/junk-displayed-when-displaying-array-of-characters) – JaMiT May 12 '22 at 01:15
  • Idk how to add a screenshot to comments. How do I manually write the \0 in this case? Do I just say idMessage[idSize-1] = '\0'; ? That would fill the 1 spot, although in the future the id may be 2 or 3 characters. I get an error if I write to [idSize], as its too big? – Sorin Jayaweera May 12 '22 at 01:17
  • @SorinJayaweera -- You are overrunning the buffer when setting your arrays. Nothing else matters until you fix that issue. – PaulMcKenzie May 12 '22 at 01:18
  • *"I got even worse output."* -- that output looks better to me. Your `idMessage` output, which is supposed to be a single `n` is now a single `n`, while in the earlier output it had two `n`s. This is a sign that your loops are working better. – JaMiT May 12 '22 at 01:18
  • YAAY thank you so much everyone, this works now. I made the two loops i < size, instead of <= size. Then I added two lines that say idMessage[idSize] = '\0'; dataMessage[dataSize] = '\0'; It didn't give me an error this time that i loaded, so I think its fine? Before I got an error bc idSize was outside of the array bc it was 1 more, index 1 when the array is 0. Thank you anyways, this is working and I'm super excited to continue working! If I may ask, why doesn't the compiler automatically add a \0 to the end of arrays? – Sorin Jayaweera May 12 '22 at 01:22
  • 2
    @SorinJayaweera -- You are still overrunning the buffer by doing `dataMessage[dataSize] = '\0';`. Arrays are 0-based. The valid indices are from `0` to `n-1`, where `n` is the total number of entries in the array. Your arrays must be at least 1 element bigger (`char dataMessage[dataSize + 1];`) than what you have, and then this should be `dataMessage[dataSize] = '\0';`. The same issue with `idMessage`. – PaulMcKenzie May 12 '22 at 01:25

0 Answers0