C++ How to save char* into array of char* []

-2

I have a byte* that I am saving into a char[]

printing the char[] I get

0,1023,1023,1000;0,0,1023,1000;1023,0,0,1000;1023,1023,0,1000;0,1023,0,1000

I pass that to a function using strtok to split on ";", saving that value to char *colorsAtTick;, then I am trying to save that into the array char *colorsAtTickArray[] = {}; by doing colorsAtTickArray[patternLength] = colorsAtTick;

But I get an error when trying to access colorsAtTickArray[i] in a loop.

The code is:

void callback(char* topic, byte* payload, unsigned int length) 
{
  //save payload bytes into char
  char pattern[length+1];
  for (int i = 0; i < length; i++) 
  {
    pattern[i] = (char)payload[i];
  }
  pattern[length+1] = '\0';
  Serial.print("this is the whole pattern: ");
  Serial.println(pattern);
  splitPatternTicks(pattern);
}

void splitPatternTicks(char *pattern) {
  char *colorsAtTick;
  char *colorsAtTickArray[] = {};
  //split pattern on ";" into chars
  colorsAtTick= strtok (pattern,";");
  while (colorsAtTick != NULL)
  {
    Serial.print("split on ; -->");
    Serial.println(colorsAtTick);
    //save the chars into an array
    colorsAtTickArray[patternLength] = colorsAtTick;

    patternLength ++;
    colorsAtTick = strtok (NULL,";");
  }
  for (int i = 0; i < patternLength; i ++) {
    //loop over the array of char to further split and save values
    //colorsAtTickArray ex. "255,0,0,1000"
    Serial.println("trying to parse values, sending ->");
    Serial.println(colorsAtTickArray[i]);
    //line above prints a blank line, then an error is thrown
    //parseTickValues(colorsAtTickArray[i], patternLength);
    //list[i] -> printObj();
  }
}

the terminal output:


This is the whole pattern:

0,1023,1023,1000;0,0,1023,1000;1023,0,0,1000;1023,1023,0,1000;0,1023,0,1000


split on ; -->0,1023,1023,1000

split on ; -->0,0,1023,1000

split on ; -->1023,0,0,1000

split on ; -->1023,1023,0,1000

split on ; -->0,1023,0,1000

trying to parse values, sending ->


Exception (28):
epc1=0x40208ccc epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000000c depc=0x00000000

stack

ctx: cont
sp: 3ffffcf0 end: 3fffffc0 offset: 01a0
3ffffe90:  40203124 3ffee720 0000000c 402033f8  
3ffffea0:  3ffee608 3ffee720 3ffee720 40203465  
3ffffeb0:  3ffee608 3ffee720 00000000 40201224  
3ffffec0:  0000000c 0000004b 3ffee720 40203448  
3ffffed0:  0000000c 3ffee720 3ffee720 3fffff40  
3ffffee0:  0000000c 3ffee720 3ffffef0 40201290 <
3ffffef0:  30312c30 312c3332 2c333230 30303031  
3fffff00:  302c3000 3230312c 30312c33 31003030  
3fffff10:  2c333230 2c302c30 30303031 32303100  
3fffff20:  30312c33 302c3332 3030312c 2c300030  
3fffff30:  33323031 312c302c 00303030 6bc6a700  
3fffff40:  00000001 00005b68 3ffee60c 4020525c  
3fffff50:  3fffdad0 00005b68 3ffee60c 4020300c  
3fffff60:  007a1201 73c993ca 3ffee700 40202e90  
3fffff70:  00000004 3ffee60f 0000005b 402010b5  
3fffff80:  00000000 00000000 3ffee60c 3ffee798  
3fffff90:  3fffdad0 00000000 3ffee60c 402011a0  
3fffffa0:  3fffdad0 00000000 3ffee758 40203b80  
3fffffb0:  feefeffe feefeffe 3ffe84e8 40100bdd  
<<<stack<<<
⸮n⸮⸮⸮⸮⸮⸮

I was testing out what im trying to accomplish in a repl but I also have issues there as well (in this example only 2 of the 3 are printed) https://repl.it/@jordanklaers/arrayOfChars

c++
arrays
asked on Stack Overflow Nov 25, 2019 by Jordan Klaers • edited Nov 26, 2019 by phuclv

2 Answers

0

You have an off by 1 error.

pattern[length+1] = '\0';

This will overwrite past the end of your array and replace information on the stack causing your error
The last element in the pattern array will be pattern[length]
You have also included a common mistake in your for loop. Please see this article for an explanation

answered on Stack Overflow Nov 25, 2019 by ron
0

Thanks to Sam Varshavchik and others who mentioned the issue I got past this by using

char *colorsAtTickArray[ticks];

where ticks is a variable representing the number of values

answered on Stack Overflow Nov 26, 2019 by Jordan Klaers

User contributions licensed under CC BY-SA 3.0