Reputation: 1077
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);
}
Called as char charssId[AP_NameString.length()+1] = string2char(AP_NameString);
What is the right way to call the function? Want to change String ssid to char ssid, to make it compatible with esp8266 library.
Upvotes: 1
Views: 2544
Reputation: 6465
You are returning pointer to first character of the array from the function and assigning it to the array, this is not possible. Learn more here.
To make it work you've got to assign a pointer returned from function to pointer variable -- like following:
char* charssId = string2char(AP_NameString);
// Do stuff
delete [] charssId;
and then access it like regular array:
charssId[index]
Thats already explained in answer above, so there are several approaches which handles this task easier.
First is toCharArray() method of string class
char opChar[ipString.length() + 1];
ipString.toCharArray(opChar, ipString.length());
// Do stuff
delete [] opChar;
Second is c_str() method of class string (string returned is const
)
const char *unmodificable = ipString.c_str();
Upvotes: 1
Reputation: 3743
char charssId[AP_NameString.length()+1] = string2char(AP_NameString);
This line is will not work. Because char charssId[AP_NameString.length()+1]
this means you are declaring an array of certain size and at the same time replacing it with the returned array from the method.
You can do as follows,
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.
Upvotes: 1
Reputation: 598134
There are many problems with your code.
Your function is declared as returning a single char
, not an array of char
s. And it is missing an actual return
statement. But even if it weren't, you would be returning a local array that goes out of scope when the function exits, leaving the caller with a dangling pointer to invalid data.
You are declaring the opChar
array in a non-standard way known as a "variable-length array". VLAs are a compiler-specific extension, and thus not portable. You need to dynamically allocate the array using new[]
instead.
Try this:
char* string2char(const String &ipString){
char *opChar = new char[ipString.length() + 1];
/*
for (int i = 0; i < ipString.length(); i++)
opChar[i] = ipString.charAt(i);
*/
ipString.toCharArray(opChar, ipString.length());
opChar[ipString.length()] = '\0';
return opChar;
}
char *charssId = string2char(AP_NameString);
// use charssId as needed...
delete[] charssId;
A safer option is to use std::string
instead:
std::string string2char(const String &ipString){
std::string opChar;
opChar.resize(ipString.length());
/*
for (int i = 0; i < ipString.length(); i++)
opChar[i] = ipString.charAt(i);
*/
ipString.toCharArray(opChar, ipString.length());
return opChar;
// alternatively:
// return std::string(ipString.c_str(), ipString.length());
}
std::string charssId = string2char(AP_NameString);
// use charssId.c_str() as needed...
But a conversion is actually not needed at all:
const char *charssId = AP_NameString.c_str();
// use charssId as needed...
Upvotes: 1