title: Arduino copy String to char * and use outside function
body: <p>I am developing program with ESP32 WIFI microcontroller where I need to scan the available nearby WiFi networks and store them in a list so I can use them later outside the function. I have initially managed to do everything easily using String type variable, but I hear a lot of bad words about using String in Arduino so I am now trying to do everything with char* which I find a little bit more challenging than String.</p>
  <p>I have found an article which describes how to convert String to char array:
  <a href="https://circuits4you.com/2018/03/08/arduino-convert-string-to-character-array/" rel="nofollow noreferrer">https://circuits4you.com/2018/03/08/arduino-convert-string-to-character-array/</a></p>
  <pre><code>// Define 
  String str = &quot;This is my string&quot;; 
  
  // Length (with one extra character for the null terminator)
  int str_len = str.length() + 1; 
  
  // Prepare the character array (the buffer) 
  char char_array[str_len];
  
  // Copy it over 
  str.toCharArray(char_array, str_len);
  </code></pre>
  <p>In my code:
  I initialise a global variable char array where I want to store my information.</p>
  <pre><code>char* list_strings[10];
  </code></pre>
  <p>Then I call function to scan networks:</p>
  <pre><code>int scan_wifi_networks(){
  
    Serial.println(&quot;scan start&quot;);
      // WiFi.scanNetworks will return the number of networks found
      int n = WiFi.scanNetworks();
      Serial.println(&quot;scan done&quot;);
      if (n == 0) {
          Serial.println(&quot;no networks found&quot;);
      } else {
          Serial.print(n);
          Serial.println(&quot; networks found&quot;);
          for (int i = 0; i &lt; 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(&quot;temp buffer is set to=&quot;);
              Serial.println(temp_buffer);
              list_strings[i] = temp_buffer;
          }
      }
      Serial.print(&quot;list of strings inside scanning function&quot;);
      for(int i=0 ; i&lt;=n ; i++){
        Serial.println(list_strings[i]); // prints a lot of garbage??
      }
      return n;
  }
  </code></pre>
  <p>The function above will scan networks and is suppose to convert the String variable (WiFi.SSID) to char array which I then store into my list:</p>
  <pre><code>list_strings[i] = temp_buffer;
  </code></pre>
  <p>My serial monitor :
  <a href="https://i.stack.imgur.com/SDpoD.png" rel="nofollow noreferrer">serial monitor image</a></p>
  <p>It is able to print out the wifi names properly in for loop, but as soon as it gets out of for loop, it is printing some garbage.</p>
  <p>Appreciate any help</p>
  <p><strong>UPDATE1</strong></p>
  <p>I am trying to print the address of temp_buffer:</p>
  <pre><code>char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
  int ptr =  &amp;temp_buffer;
  Serial.print(&quot;Pointer :&quot; );
  Serial.println(ptr);
  </code></pre>
  <p>But with no luck. I am getitng a invalid conversion from char* to int error</p>
  <p><strong>UPDATE2</strong></p>
  <p>I have cast my temp_buffer to int and that compiles, however, I do not know if that is correct or not.</p>
  <pre><code>            char temp_buffer[str_len]; // create a temporary storage to copy the string to char array
              int ptr =  (int)&amp;temp_buffer;
              Serial.print(&quot;Pointer :&quot; );
              Serial.println(ptr);
  </code></pre>
  <p>The serial monitor:</p>
  <pre><code>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
  
  </code></pre>
  <p>For some weird reason, the pointer changing from 1073422080 to 1073422096 every now and then</p>