Can I do this with a non const array

Yesterday, with your help I learned how to use a pointer array of constant words of varying length. But do the same concepts apply with an array of non constant items?

I would like to do the same thing with code that I use in all my wifi programs to save the stuff I normally send to serial print and list them into html so I can monitor what a particular program is doing.

But here's the problem, the way I am doing it is storage wasteful as all table entries use the same amount of space and often they need only a fraction of that.

To explain, here's the layout:

const unsigned int serialRows    = 30;  //define dimensions
const unsigned int serialColumns = 60;
char serialCharArray[serialRows][serialColumns] = {};//stores entries
char SerTxt[serialColumns]                  = {};//
unsigned long  SerNbr;
char SerTime[9]       = {};//time formatted in clock read section.

SerTxt is anything I strcpy to it as the program runs.

I then go to SerOut or SerOutN if I have a number I want to include.


void SerOutN() {
  AddNumbers = true;
  SerOut();
}
void SerOut() {
  ReadTheClock();//refresh SerTime
  for (unsigned int i = serialRows - 1; i > 0; i--) {//move elements down one.
    strcpy(serialCharArray[i], serialCharArray[i - 1]);
  }
  strcpy(serialCharArray[0], SerTime);//start new element
  strcat(serialCharArray[0], " ");
  strcat(serialCharArray[0], SerTxt);
  if (AddNumbers) {
    AddNumbers = false;
    strcat(serialCharArray[0], " = ");
    ltoa(SerNbr, serialCharArray[0] + strlen(serialCharArray[0]), 10);
  }
  if (SwStatus[Messaging]) {
    Serial.println(serialCharArray[0]);
  }
}

Then to print this in html format I have,

 const char *ctdctr[]              = {"</td></tr>"};                // close td close tr 
 const char *otrotd[]              = {"<tr><td>"};                  // open tr open td
          
auto AddToTable = [&] () {
              strcat(html, *ctdctr);
              client.print(html);
              strcpy(html, *otrotd);
            };
//      t e x t   m e s s a g e s
            client.print(F("</table><table border=7 style='text-align:left'align='center'>"));
            for (const char *SerialOutput : serialCharArray) {
              strcat(html, SerialOutput);
              AddToTable();
            }

So is it possible to have variable length array elements?

I can't speak to the code, but just generally, many people don't realize that they can generate HTML serially. You normally don't need to concatenate HTML content that you are creating and sending. You can generate it and emit it dynamically, one character or one string at a time. It usually saves memory.

I've thought of that, but wouldn't repetitive client.print statements cause their own overhead?

No, because you'd factor them.

Do what?

You'd factor the common phrases into related functions or macros, so repetition was minimised.

There is a certain amount of code generated with each call to print, but there is also code generated when concatenating text together to form a single string to use with print. Most arduino's have a very limited amount of dynamic memory (ram), so it is usually preferably to avoid the buffer associated with forming a single long text string. Concatenating text also introduces the risk of writing past the end of the buffer if one of the items has an unexpectedly long length.

1 Like

I still want to save the messages for re display every time enter is hit.

I think I will try a large char array and just strcat into it with say a \n on the end of each line, then use \n to separate it all back into html lines for display.

Just print it as-is and let the newline characters ('\n') separate it into lines for display.

not sure that will work with html tags. for serial.print I will I guess but don't you also need a \l?

A '\l'?

What's that?

I meant linefeed

/r ok

HTML doesn't care about line endings. '\n' (newline/linefeed), '\r' (carriage return), and '\t' (tab) are treated just like a space.

Oh, then I don't have to worry, thanks

Just made the absolute dumbest mistake you can make, forgot the == thing. Spent two hours wondering what one earth was going on.

You can enable warnings for that. I'm pretty sure it flags them.

it did, I was too busy looking at the byte count

The IDE really needs something like a "lint"