I am trying to rewrite a project that works for a few hours on a MKR WiFi 1010 and then just freezes. Some googling suggested that my use of the String class was likely causing heap crashes. So I would like to learn about c-string functions and better memory management.
The following code will not compile and throws an error at line 22 that reads "invalid conversion from 'char*' to 'char' [-fpermissive]"
Line 22 is "ptrValueLocation = strchr(GetPayload, srchCmd);"
I am having much confusion understanding when to use the 'char' and 'char *variable' declarations as well as how to properly use pointers to char variables.
Here is the code:
// Sandbox for learning string functions
// ***** BEGIN DECLARATIONS *****
char GetPayload[40] = {"Door Command 1 Door Status Open"}; // Door Command is tinyint and can be either 1 or 2. Door Status is varchar[10] and values can be either Open or Closed.
char ptrValueLocation;
char CurrentStatus[2] = {'C'};
char srchCmd = {'S'};
// ***** END DECLARATIONS *****
void setup() {
Serial.begin(9600);
delay(2000);
} // for the setup
void loop() {
ptrValueLocation = strchr(GetPayload, srchCmd);
Serial.print(F("Here is the first character of the current status: "));
strncpy(CurrentStatus, ptrValueLocation + 7, 1);
Serial.println(CurrentStatus);
Serial.println();
// do nothing forevermore:
while (true);
} // for the void loop
I appreciate any help in understanding these issues. I have spent the better part of two days reading C documentation and forum posts and still am going in circles with tweaks and re-compiles.
Ron