How to pass a String to a Char Pointer

Hello, I need to pass two string variables (router, pws) to two char pointer variables (ssid, assword), I am using this code:

char* ssid = "";
char* password = "";

String router = "ThisMyRouter";
String psw = "12345678";
int str_len;

void setup() {
  Serial.begin(115200);
  Serial.println();

  str_len = router.length() + 1;
  ssid[str_len];
  router.toCharArray(ssid, str_len);
  Serial.println(ssid);

  str_len = psw.length() + 1;
  password[str_len];
  psw.toCharArray(password, str_len);
  Serial.println(password);
}

void loop() {}

but it does not run correctly, the data is repeated:

ThisMyRouter
ThisMyRouter12345678
12345678

Someone could tell me how to solve this?

Tell us what you need to do, not how you think you ought to do it

I need to pass two string variables (router, pws) to two char pointer variables (ssid, assword)

Those two pointers are pointing to static data with no memory allocated

Try

char ssid[33];
char pwd[64];

Then you have SRAM to store your information

But if you only need the cString pointer of your Strings, use router.c_str() and psw.c_str()

No, that's how you think you need to do what you want to do.

(And they're Strings, not strings. I believe I've already hinted this)

This line

ssid[str_len];

does absolutely nothing.

This might work

ssid = (char *) malloc(str_len);

I hope that fixed it for you. But even if it does, I think this is probably not the right or best way to do it. It feels pointless. But I can't be sure.

You have asked what is called an "X-Y" question. You want do to X and you think you need to do Y to achieve it, but you can't make Y work. So you ask the forum how to fix Y.

But if you ask the forum how to achieve X, there might be an easy way that does not require Y to be done at all!

Forgetting the code I have posted, what is the best way to pass a String variable to a pointer?

.c_str()

You could pass a pointer to the actual char array in the String, but there is no way to actually pass a String to a pointer, a pointer just points to something else, it cannot contain any actual data itself. You also need to be careful setting a pointer to anything having to do with a String, because Strings tend to get dynamically moved around in memory if you start manipulating them.

You can store the String in a char array, then set a pointer to point to that char array, but then why not just store the text in a char array to begin with.

Forgetting the code that you have posted, why do you need to do it ? Where does the String come from and could it be a C style string instead ?

I am trying to implement a web server with ESP8266, here are the parameters: ssid and password are defined as char*:

char* ssid = "your-ssid"
char* password = "your-password"

but I need to change this data, programmatically within my code, I get this data from the serial port in the form of strings, hence my need to pass from strings to pointers

I think you mean that you get the data as Strings, but of course you don't have to, you could get them as pointers to C style strings if you wanted to

Could you please give me an example?

Start here