[Solved]- Storing strings into an array - Basic programming question

Hi there.
I working in a code that, when the Particle Cloud is not available, stores an hourly measured value
into an array. If the array is full, the oldest value is lost, and when the Particle Cloud is available again
it sends it the stored values.

I successfully tested the code using a float array to store the measured values (that are, of course, floats).
When the Particle Cloud is available, the code creates a string that includes an identifier, time stamp and the measured value to hit a webhook.

Since when I creating the abobe mentioned string, I successfully used:

        strcat (WHTemp, String (Measured_Value,3));

to send the measured value with only 3 decimal places into the webhook, I was wondering if instead of creating an array of floats, I can use an arrays of “strings” to save memory space.

First, I will like to know if I will be saving memory doing that and second how to do it :smile:
I wrote this so far:

  char* power[100];
  ...

  char inter[25];

  strcpy (inter, String (Measured_Value,3));
  power[0] = inter;

but my logic could be completely wrong.
I will appreciate your help.
Thank you!

@gavpret, power[100] is an array of pointers to chars, not char arrays. Second, I suggest you use sprintf() instead os String() for creating your text string since String dynamically allocates memory and can create heap fragmentation. The line power[0] = inter; will work fine but will you be declaring 100 arrays like inter[]? That alone would take 2500 bytes of storage. Also don’t forget that there is a limit 255 bytes you can send in a single publish.

It is actually more efficient to store your floats in an array since each takes 4 bytes (total of 400 bytes). Only when you publish will you then build your payload using sprintf() and/or other c-string commands (to prevent heap issues).

1 Like

Thank you @peekay123 for the kind reply.

I will stick to the array of floats and start using sprintf().

Have a great day!

1 Like

Thanks for the help @peekay123!

1 Like