When you declare an array and don't define the values at the same time you must tell the compiler how many elements the array is required to hold so that it can allocate the required memory based on the number of elements and their type
So, something like this
char anArray[8]; //an array with room for 8 chars
int anArray[10]; //an array with room for 10 ints[/code]
Your code does this
char chr_num[0]; //an array with room for zero chars
UKHeliBob:
When you declare an array and don't define the values at the same time you must tell the compiler how many elements the array is required to hold so that it can allocate the required memory based on the number of elements and their type
So, something like this
char anArray[8]; //an array with room for 8 chars
int anArray[10]; //an array with room for 10 ints[/code]
Your code does this
char chr_num[0]; //an array with room for zero chars
but in my case, the array values in unknown, it depends how many message being sent
Try using my SafeString library which will let you print() to your packet and give you an error message if you try to add too much data
Try
#include "SafeString.h"
// download SafeString library from Arduino library manager
// or from the tutorial page
// https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
void setup() {
Serial.begin(9600);
for (int i = 10; i > 0; i--) {
Serial.print(' '); Serial.print(i);
delay(500); //
}
Serial.println();
SafeString::setOutput(Serial); // enable error msgs
}
createSafeString(sfRadioPacket, 25); // space for 25 chars
int packetNum = 5676;
void loop() {
sfRadioPacket = ""; // clear it when needed
sfRadioPacket.print(packetNum); // you can print to a SafeString
// print your other data here
// SafeString will tell you if you overflow the packet
Serial.println(sfRadioPacket.c_str()); // print the underlying char[]
// send it to your radio library
// radio.send(sfRadioPacket.c_str(),sfRadionPacket.length());
while (1) {} // stop here
}
Well it depends how you are going to use it.
There is a mixture here of char[] (for the packet) and '\0' terminated string arrays for sprintf
Easy to confuse the two.
That off-by-one error is one of the errors SafeString avoids (guards against)
createSafeString(sfRadioPacket, 25); // space for 25 chars
underneath creates a char[26] to allow for the terminating '\0' for the string
Then when you get to the
radio.send( ) the sfRadioPacket.length() only counts the chars not the terminating string '\0'