Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Arduino将字符串复制到char*并使用外部函数_C++_String_Arduino_Char - Fatal编程技术网

C++ Arduino将字符串复制到char*并使用外部函数

C++ Arduino将字符串复制到char*并使用外部函数,c++,string,arduino,char,C++,String,Arduino,Char,我正在用ESP32 WIFI微控制器开发程序,我需要扫描附近可用的WIFI网络,并将它们存储在列表中,以便以后在功能之外使用它们。我最初尝试使用String类型变量轻松地完成所有工作,但我听到很多关于在Arduino中使用String的不好的词,所以我现在尝试使用char*完成所有工作,我发现这比String更具挑战性 我发现一篇文章描述了如何将字符串转换为字符数组: 在我的代码中: 我初始化了一个全局变量char数组,我想在其中存储我的信息 char* list_strings[10];

我正在用ESP32 WIFI微控制器开发程序,我需要扫描附近可用的WIFI网络,并将它们存储在列表中,以便以后在功能之外使用它们。我最初尝试使用String类型变量轻松地完成所有工作,但我听到很多关于在Arduino中使用String的不好的词,所以我现在尝试使用char*完成所有工作,我发现这比String更具挑战性

我发现一篇文章描述了如何将字符串转换为字符数组:

在我的代码中: 我初始化了一个全局变量char数组,我想在其中存储我的信息

char* list_strings[10];
然后我调用函数扫描网络:

int scan_wifi_networks(){

  Serial.println("scan start");
    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            
            int str_len = WiFi.SSID(i).length() + 1;  // this is to know the the length of the string +1
            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
            WiFi.SSID(i).toCharArray(temp_buffer, str_len); // converts string to char array 
            Serial.print("temp buffer is set to=");
            Serial.println(temp_buffer);
            list_strings[i] = temp_buffer;
        }
    }
    Serial.print("list of strings inside scanning function");
    for(int i=0 ; i<=n ; i++){
      Serial.println(list_strings[i]); // prints a lot of garbage??
    }
    return n;
}
我的串行监视器:

它能够在for-loop中正确打印wifi名称,但一旦退出for-loop,它就会打印一些垃圾

谢谢你的帮助

更新1

我正在尝试打印临时缓冲区的地址:

char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
int ptr =  &temp_buffer;
Serial.print("Pointer :" );
Serial.println(ptr);
但是没有运气。我得到一个从char*到int的无效转换错误

更新2

我已经将我的temp_缓冲区转换为int并编译,但是,我不知道这是否正确

            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
            int ptr =  (int)&temp_buffer;
            Serial.print("Pointer :" );
            Serial.println(ptr);
串行监视器:

Pointer :1073422080
temp buffer is set to=Telia-33F8A3-Greitas
Pointer :1073422080
temp buffer is set to=HUAWEI-STOLYARCHUK
Pointer :1073422080
temp buffer is set to=Telia-E619A5-Greitas
Pointer :1073422080
temp buffer is set to=Telia-ED7E29-Greitas
Pointer :1073422096
temp buffer is set to=MEZON_1156F3
Pointer :1073422080
temp buffer is set to=Carolyne’s iPhone
Pointer :1073422080
temp buffer is set to=Teo-E6A379-Greitasis
Pointer :1073422096
temp buffer is set to=HH40V_BE2F
Pointer :1073422096
temp buffer is set to=Fishman
Pointer :1073422096
temp buffer is set to=MEZON_9EE8
Pointer :1073422096
temp buffer is set to=4G-Gateway-4981
Pointer :1073422080
temp buffer is set to=HUAWEI-B525-C466
Pointer :1073422096
temp buffer is set to=DGD
Pointer :1073422096
temp buffer is set to=4G-Gateway-D29C
Pointer :1073422096
temp buffer is set to=WIFI 2021
Pointer :1073422080
temp buffer is set to=Telia-E79C95-Greitas
Pointer :1073422080
temp buffer is set to=Telia-E5E1B9-Greitas
Pointer :1073422096
temp buffer is set to=#Telia-1E6E7A
list of strings inside scanning function


出于某种奇怪的原因,指针时不时地从1073422080变为1073422096

问题在于
字符临时缓冲区[str_len]。这会在堆栈上分配内存(几乎可以肯定的是,for循环的每次迭代都会分配相同的内存位。我很惊讶这会被编译,因为我预期会收到一个错误,抱怨stru_len不是常量。
在任何情况下,您都在保存堆栈区域的地址,该地址在每次迭代时以及退出循环后都可重复使用。
在每次迭代中打印temp_缓冲区的地址可能会提供信息

您可以改为使用new()在堆上分配内存,并记住在完成时使用delete()释放内存


更好的方法是使用STL中的字符串和向量。

您想将字符串存储在哪里?我想将信息存储在我创建的全局变量中:
char*list_strings[10]
那只是一个指针数组。它只存储指向其他变量的指针。哪个变量应该保存字符串?对不起,我没有正确理解你的意思:(你是在问我在将字符串复制到数组之前存储字符串的临时变量吗?
char*list_strings[10]
无法保存任何字符串。它只能保存指向字符串的指针。谢谢你的回答。不幸的是,在Arduino IDE上打印tempo_缓冲区的地址并不像我希望的那样简单。我无法让它工作。你知道如何打印temp_缓冲区的地址吗?我会调查malloc,因为我没有ex还有,我从来没有听说过STL。我已经更新了我最初的问题UPDATE1。我在那里发布了一些代码。我不知道如何在注释中发布代码。问题是,您正在将字符串复制到temp_缓冲区,temp_缓冲区是一个局部变量,用于寻址堆栈上的某个区域,该区域在每次迭代结束时将超出范围在每次迭代中覆盖几乎相同的堆栈区域,然后将该地址保存到列表字符串[]。当您开始查看列表字符串[]时该堆栈位可能正在用于其他用途。即使尚未重复使用,也只会包含您最后写入的内容。我认为您需要阅读C/C++中的内存管理忽略对malloc()的引用,我应该说new()好的,我明白了,但是这个问题是如何用strcpy而不是=?这有什么不同?
            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
            int ptr =  (int)&temp_buffer;
            Serial.print("Pointer :" );
            Serial.println(ptr);
Pointer :1073422080
temp buffer is set to=Telia-33F8A3-Greitas
Pointer :1073422080
temp buffer is set to=HUAWEI-STOLYARCHUK
Pointer :1073422080
temp buffer is set to=Telia-E619A5-Greitas
Pointer :1073422080
temp buffer is set to=Telia-ED7E29-Greitas
Pointer :1073422096
temp buffer is set to=MEZON_1156F3
Pointer :1073422080
temp buffer is set to=Carolyne’s iPhone
Pointer :1073422080
temp buffer is set to=Teo-E6A379-Greitasis
Pointer :1073422096
temp buffer is set to=HH40V_BE2F
Pointer :1073422096
temp buffer is set to=Fishman
Pointer :1073422096
temp buffer is set to=MEZON_9EE8
Pointer :1073422096
temp buffer is set to=4G-Gateway-4981
Pointer :1073422080
temp buffer is set to=HUAWEI-B525-C466
Pointer :1073422096
temp buffer is set to=DGD
Pointer :1073422096
temp buffer is set to=4G-Gateway-D29C
Pointer :1073422096
temp buffer is set to=WIFI 2021
Pointer :1073422080
temp buffer is set to=Telia-E79C95-Greitas
Pointer :1073422080
temp buffer is set to=Telia-E5E1B9-Greitas
Pointer :1073422096
temp buffer is set to=#Telia-1E6E7A
list of strings inside scanning function