strange question, need some help, thanks

My board is ESP8266.
In the smartConfig() function, get the ssid and password, print them(display the right infomation i want) and then pass the value to the external variables.
In the loop function, print the ssid, but the result is something like "??".

so what is the problem, i have spent much time on it. thank you very much if you can give me some information about it.

here is my code

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
const char* ssid;
const char* password;

bool smartConfig(){
    WiFi.mode(WIFI_STA);
    Serial.println("\r\nWait for Smartconfig");
    WiFi.beginSmartConfig();
    while (1){
        delay(500);
        Serial.println("waiting for smartconfig");
        if (WiFi.smartConfigDone()){
            delay(1000);
            ssid= WiFi.SSID().c_str();
            password = WiFi.psk().c_str();
            Serial.println(ssid); // the right result like "mywifi"
            Serial.println(password); // the right result like "mypasswd"
             Serial.println("SmartConfig Success");
            break;
        }
    }
return true;
}
 
 
void setup() {
    Serial.begin(115200);
    Serial.println("");
    delay(1000);
    smartConfig();
    Serial.println(ssid);
    Serial.println(password);
}
 
 
void loop() {
    delay(1000);
    Serial.println(ssid);// not the "mywifi",but "?????"
    Serial.println(password);//not the "mypasswd",but "?????"
}

Why a while(1) and a break on success? Why not :

while(!WiFi.smartConfigDone())!
// And then once it is successful
// all the stuff inside that if statement

Why do you have that function returning a bool and all it every returns is true? Why is it not declared as void in that case? The return value you have now doesn't mean anything and you don't even use it.

ssid= WiFi.SSID().c_str();

What does WiFi.SSID return? Are you setting ssid equal to a pointer that isn't going to point at anything anymore once that function exits?

i just post the simplified code, delete the unrelated.

the WiFi.SSID() return "A String containing the SSID the WiFi shield is currently connected to"

here is the link WiFi - Arduino Reference

the strange thing is that, in the smartConfig function, the print result is right, but the loop function is not

ericwgq:
the strange thing is that, in the smartConfig function, the print result is right, but the loop function is not

Yeah, that's why I suspect a pointer going out of scope. I don't know that I would trust a library written for something as hungry as WiFi support on a microcontroller that made extensive use of the String class.

OK, so look at the WiFi.cpp file and that function is here:

char* WiFiClass::SSID()
{
    return WiFiDrv::getCurrentSSID();
}

Looks like it is returning a char array already. So you shouldn't need the c_str() call. Just use it as is.

Or better yet, don't just assume that the WiFi library is going to keep it in scope for you and use strcpy to copy that string into a char array in your program that you have control over.

ssid = WiFi.SSID();// error can not convert String to const char*.

the strcpy works, thank you very much!!!