I’m having a weird problem to copy the part of a char* to another char*, it looks like the copy is changing the contents of the source char*.
This is part of my code:
void setValuesParamsList(char* bluetoothString) {
int lastPosition = 0;
int endPosition = 0;
boolean param = false;
boolean value = false;
int lengthChar = 0;
char* paramString;
char* valueString;
Serial.println(bluetoothString);
for(int i=0; i<strlen(bluetoothString); i++) {
Serial.println("Percorrendo");
// procura pelo '='
if(bluetoothString[i] == '=') {
Serial.println("->Param");
endPosition = i-1;
param = true;
value = false;
} else if(bluetoothString[i] == '#' || i == (strlen(bluetoothString)-1)) {
Serial.println("->Value");
endPosition = bluetoothString[i] == "#" ? i-1 : i;
param = false;
value = true;
}
if(param == true && value == false) {
lengthChar = endPosition - lastPosition;
delete[] paramString;
paramString = new char(lengthChar+1);
int l=0; // contador para popular o char*
for(int j=lastPosition; j<=endPosition; j++) {
// insere os dados no vector
paramString[l++] = bluetoothString[j];
}
paramString[lengthChar+1] = '\0';
Serial.println(F("-------------------------------"));
Serial.println(paramString);
Serial.println(String(strlen(paramString)));
Serial.println(bluetoothString);
Serial.println(String(strlen(bluetoothString)));
Serial.println(F("-------------------------------"));
lastPosition = i+1;
param = false;
value = false;
}
}
}
This is what appears on the serial monitor:
action=getData#time=111111
Percorrendo
Percorrendo
Percorrendo
Percorrendo
Percorrendo
Percorrendo
Percorrendo
->Param
-------------------------------
action
6
on
2
-------------------------------
The idea is to read the parameters and values of the parameters from char * “action=getData#time=111111”, but it seems that the copy of part of the char * affects the original value and stops the main FOR.
Am I copying the snippet the wrong way?