Hi there, so I have a function in my .cpp file that is declared in a .h file and I am calling it in the .ino sketch with something like this:
char ID = "blah123";
float value = 123.45;
myFunction(ID, value);
so the input to the function is String and float. (However, I will take any suggestions as to what I should pass to the function). In the .cpp file I have something like this:
void myClass::myFunction(char ID, float value) {
char charBuffer[64];
sprintf(charBuffer, "Device ID is %s and value is %d", ID, value);
Serial.print("charBuffer: ");
Serial.println(charBuffer);
Serial.print("charBuffer size: ");
Serial.println(sizeof(charBuffer)); // This prints out 100 but I would like to get the actual size neglecting empty spaces in the buffer
}
I suppose you could test this in a single .ino sketch like this:
void setup() {
Serial.begin(9600);
char ID = "blah123";
float value = 123.45;
myFunction(ID, value);
}
void loop() {
// Nothing here
}
void myFunction(char ID, float value) {
char charBuffer[64];
sprintf(charBuffer, "ID is %s and value is %d", ID, value);
Serial.print("charBuffer: ");
Serial.println(charBuffer);
Serial.print("charBuffer size: ");
Serial.println(sizeof(charBuffer)); // This prints out 100 but I would like to get the actual size neglecting empty spaces in the buffer
}
I am expecting something like "charBuffer: ID is blah123 and value is 123.45" but instead I get "charBuffer: ID is and value is -6554". Also, sizeof(charBuffer) obviously returns 64 because I had to set it's size, but I would really like to find out the actual size that is being occupied in the buffer. If I just say "char charBuffer[]" it gives me an error saying that the buffer size is unknown. I basically want a String that is of the following form (logically; the code below doesn't work):
String output2 = "ID is " + String(ID) + " and value is " + String(value);
Serial.println(output2);
But that gives me "ID is a and value is 123.45". Why would String(ID) give me "a" instead of "blah123"? Ultimately my goal is to get a char array but I know how to use string.toCharArray() to get a char array from a String.
Why am I not getting what I'm expecting? Thanks!