char string2char(String ipString){
char opChar[ipString.length() + 1];
memset(opChar, 0, ipString.length() + 1);
for (int i = 0; i < ipString.length(); i++)
opChar[i] = ipString.charAt(i);
}
称为
char charssId[AP_NameString.length()+1] = string2char(AP_NameString);
调用函数的正确方法是什么?想要将String ssid更改为char ssid,使其与esp8266库兼容。
最佳答案
这行是行不通的。因为char charssId[AP_NameString.length()+1]
,这意味着您要声明一个特定大小的数组,同时将其替换为方法中返回的数组。
您可以执行以下操作,
char* string2char(String ipString){ // make it to return pointer not a single char
char* opChar = new char[ipString.length() + 1]; // local array should not be returned as it will be destroyed outside of the scope of this function. So create it with new operator.
memset(opChar, 0, ipString.length() + 1);
for (int i = 0; i < ipString.length(); i++)
opChar[i] = ipString.charAt(i);
return opChar; //Add this return statement.
}
// Now call this as below,
char* charssId = string2char(AP_NameString); // make the variable as pointer so that it can hold an array address.
// use it as a char array.
delete[] charssId; // Remember to delete it after finished using it.
关于c++ - 在Arduino中将字符串转换为char,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51531033/