My goal is to create a function that concatenate 2 chars into 1, and return it as MQTT subscribe. I get a difference between the results inside and outside the function ( I'm new to Arduino ), and I can't find why:
Code:
// Update variable below:
const char *direction = "up";
const char* clientID = "Sonoff";
//Consts
const char *client_temp = "HomePi/Dvir/Windows/";
const char* outTopic = "HomePi/Dvir/Messages";
//const char* outTopic2 = "HomePi/Dvir/Windows/ESP32";
const char* inTopic2 = "HomePi/Dvir/Windows/All";
char *inTopic;
void setup() {
Serial.begin(9600);
// len(create_topic(client_temp, clientID));
inTopic = create_topic(client_temp, clientID);
Serial.println(inTopic);
}
char *create_topic(char *chr1, char *chr2){ . // added *
char topic[strlen(chr1) +strlen(chr2)+1];
sprintf(topic,"%s%s",chr1,chr2);
Serial.println(topic);
return topic;
}
void loop() {
// put your main code here, to run repeatedly:
}
Result ( both line should have been same):
HomePi/Dvir/Windows/Sonoff
⸮⸮,茗⸮;⸮_⸮%⸮l?⸮az⸮z⸮%⸮⸮⸮\;⸮m⸮