Arduino Hostname Generator
14 hours ago, Aelita Sophie said:<snip>
char* hostName = "P1Meter";
"P1Meter" is a string literal, stored in read only memory (Or memory ear-marked as read only at least). You should not try and modify it, as that causes undefined behavior, yet you do:
char* newname = strcat(hostName, deblank(MAC_Adress));
"strcat" will try to append a copy of whatever string "deblank(MAC_Adress)" returns to the string pointed to by "hostName", thus trying to modify the read only literal. For that reason one should always make such string literals, that you do not intend to modify, const:
const char* example = "The quick brown fox, whatever...";
That would've caught the error at compile time.
Secondly, you should make sure the destination string has enough room to hold the complete string, which is also not the case.
To fix it,make "hostName" a array, with enough spare room to hold the entire resulting string:
char hostName[25] = "P1Meter"; //25 is just a example here, change this to your needs. //Do not forget to allow one extra space for the terminating 0.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now