Hello.
I'm having trouble with a code using arrays.
These are short programs that illustrate the problem.
I'm using an ESP8266 12E.
The first code works correctly, printing the correct values in both the first and second parts.
String projNome = __FILE__; // Project name
unsigned long logTime; // Work reg
unsigned long intervalo = 2; // convert time
char* reads[] {"0", "0", "0", "0"}; // Read reg
//---------------------------------------
void setup(void) {
Serial.begin(115200); // Inicialize serial at 115200
Serial.println(" "); // Cr Nl
Serial.println(projNome); // Project name
}
//--------------------------------------------------
void loop() {
if (millis() - logTime > (intervalo * 1000)) { // Time to convert
logTime = millis(); // Refresh time
for (int k = 0; k < 4; k++) { // Work 4 times
String temp_c = "22.55"; // Default value
int varSize = temp_c.length() + 1; // Calc size variable
char* arr = new char[varSize]; // New array *
strcpy(arr, temp_c.c_str()); // Store new value
reads[k] = arr; // Save new value at read[] array by k
Serial.print("read_1 "); Serial.print(k);
Serial.print(": "); Serial.println(reads[k]); // Print read[] array by k
}
for (int m = 0; m < 4; m++) {
Serial.print("read_2 "); Serial.print(m);
Serial.print(": "); Serial.println(reads[m]); // Print read[] array by m
}
Serial.println(" ");
}
}
First part
read_1 0: 22.55
read_1 1: 22.55
read_1 2: 22.55
read_1 3: 22.55
Second part
read_2 0: 22.55
read_2 1: 22.55
read_2 2: 22.55
read_2 3: 22.55
The second code prints the second part incorrectly, even though both printouts come from the same array. Part 1
String projNome = __FILE__; // Project name
unsigned long logTime; // Work reg
unsigned long intervalo = 2; // convert time
char* reads[] {"0", "0", "0", "0"}; // Read reg
//---------------------------------------
void setup(void) {
Serial.begin(115200); // Inicialize serial at 115200
Serial.println(" "); // Cr Nl
Serial.println(projNome); // Project name
}
//--------------------------------------------------
void loop() {
if (millis() - logTime > (intervalo * 1000)) { // Time to convert
logTime = millis(); // Refresh time
for (int k = 0; k < 4; k++) { // Work 4 times
String temp_c = "22.55"; // Default value
int varSize = temp_c.length() + 1; // Calc size variable
char str[varSize]; // New array
char *arr { str }; // New array *
temp_c.toCharArray(str, varSize); // Store new value
reads[k] = arr; // Save new value at read[] array by k
Serial.print("read_1 "); Serial.print(k);
Serial.print(": "); Serial.println(reads[k]); // Print read[] array by k
}
for (int m = 0; m < 4; m++) {
Serial.print("read_2 "); Serial.print(m);
Serial.print(": "); Serial.println(reads[m]); // Print read[] array by m
}
Serial.println(" ");
}
}
read_1 0: 22:55
read_1 1: 22:55
read_1 2: 22:55
read_1 3: 22:55
Part 2.
read_2 0: 22:5T⸮⸮?
read_2 1: 22:5T⸮⸮?
read_2 2: 22:5T⸮⸮?
read_2 3: 22:5T⸮⸮?
Since I'm a very basic programmer and learning on my own,
I couldn't understand the reason for the incorrect printout.
I would greatly appreciate any help in resolving this error.
Thank you in advance.