1
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.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Dr.PB
  • 959
  • 1
  • 13
  • 34
  • 2
    Can you describe what the function should do? Looks like stripping the characters one by one from a String object and placing them in a character array. However, the function declaration shows it returns a char, but has no return statement. Your example use shows you expect it to return an array of characters. Can you rethink or describe better what is your question? – bcperth Jul 26 '18 at 04:52
  • seems rather superfluous to do a `memset` when you anyway overwrite every byte except the last one. however, as your function stands now it doesn't do anything since it doesn't return the results from the function. – AndersK Jul 26 '18 at 05:16
  • @joH1, no it's c++ – Jabberwocky Jul 26 '18 at 06:43

3 Answers3

1
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.
Sumsuddin Shojib
  • 3,583
  • 3
  • 26
  • 45
1

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();
kocica
  • 6,412
  • 2
  • 14
  • 35
1

There are many problems with your code.

Your function is declared as returning a single char, not an array of chars. 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...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770