Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we change const char* arguments during the application running? #25

Open
kamotsuru opened this issue May 16, 2021 · 0 comments
Open

Comments

@kamotsuru
Copy link

I want to change the value of some const char* arguments of syslog() constructor during the application running.

The following is some parts of my code.
Inside loop(), I copied a string derived from the serial input to std::string object. If I create const char* variable "testapp" and put it to syslog.appName() like the following, it works.
However, If I put the copied std::string object with the same string "testapp" replacing with the commented out // line, the library sends other value than "testapp". Even if I put the same string "testapp" multiple times, it changes. It seems to refer the different memory address's value.

This may be c++ programing issue, but could you let me know how to solve this if possible?

//global scope
Syslog syslog(udpClient, server, SYSLOG_PORT, "esp32", LOG_KERN);

void loop() {
...
if (Serial.available() > 0) {
String str = Serial.readStringUntil('\n');
if (str[0] == 'R') {
...
} else if (str[0] == 'A') {
std::string para = myOffset(str, 2);
const char* param = "testapp";
Serial.println(strcmp(para.c_str(), param));
// syslog.appName((const char*)(para.c_str()));
syslog.appName(param);
}
}
...
syslog.logf(LOG_INFO, "%s %d %f %f", d.getAddress().toString().c_str(), id, current, voltage);
}

std::string myOffset (String string, int offset) {
std::string para = "";
for (int i = offset; i < string.length(); i++) {
para += string[i];
}
return para;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant