[Solved] Use of c++ array of char arrays (char**) with Photon

Hello everyone!

i am trying to print out a char** array but I always get red flashing back.

void parseHeader (char * line) {
  static int len;
  static char * tok;
  static char ** call;
  int i=0;
  tok = strtok (line,"!?:=");
  while (tok != NULL)
  {
    call[i]=tok;
    i++;
    Serial.write(tok);
    Serial.write(call[0]);
    tok = strtok (NULL, "!?:=");
  }
}

I debugged the code above and seems to be the action of printing it.
Also tried to use pure printf() but photon compile without creating binaries.

What am I doing wrong?
Thanks!

You’re not allocating the call array so you’re overwriting random memory when you set call[i].

Something like this would work:

void parseHeader (char * line) {
  const size_t MAX_CALLS = 10;
  int len;
  char * tok;
  char *call[MAX_CALLS];
  int i=0;
  tok = strtok (line,"!?:=");
  while (tok != NULL && i < MAX_CALLS)
  {
    call[i]=tok;
    Serial.write(tok);
    Serial.write(call[i]);
    i++;
    tok = strtok (NULL, "!?:=");
  }
}
1 Like

WOW! Super fast reply!
Ouch :sweat_smile:. My bad…
Thank you very much for your help.

2 Likes