Cannot add char arrays into char array list

I have an ESP8266 and there is no problem with any of its libraries.
The problem appears when trying to add char arrays into a char array list

...
 char ssids[] PROGMEM = {
  "dddd\n"
};
#include <ESP8266WiFi.h>
void setup()
{


  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  int n = WiFi.scanNetworks();
  int ssidL = sizeof(ssids) / sizeof(ssids[0]);
  for (int i = 0; i < n;)
  {
    //const *char c1 = WiFi.SSID(i).c_str()
    char charBuf[50];
    WiFi.SSID(i).toCharArray(charBuf, 50);
    ssids[i + ssidL] = charBuf;
    i++;
  }

...

}

With the code above appears an error


script:137:24: error: invalid conversion from 'char*' to 'char' [-fpermissive]
  137 |     ssids[i + ssidL] = charBuf;
      |                        ^~~~~~~
      |                        |
      |                        char*
exit status 1
invalid conversion from 'char*' to 'char' [-fpermissive]

It's desperately trying to tell you the problem - 'charBuf' is a pointer to an array. You are trying to assign it to a char.

so what could i do to solve it

Normally, I would say make 'ssids' a two dimensional array, but I also see it's PROGMEM. So what the heck are you trying to do?

strcat_P?

Edit: or is that only for AVR?

I think you intended something like this:

char *ssids[20];  // Pointers to up to 20 SSID's

#include <ESP8266WiFi.h>
void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  int n = min(WiFi.scanNetworks(), 20);
  
  for (int i = 0; i < n; i++)
  {
    ssids[i] = WiFi.SSID(i).c_str();
  }

}

This might work unless the WiFi library re-uses the space it allocated for the SSID String's. If you want the SSID's to be saved you will need to allocate space for them. The easiest method might be to use String:

String ssids[20];  // Room for up to 20 SSID's

#include <ESP8266WiFi.h>
void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  int n = min(WiFi.scanNetworks(), 20);
  
  for (int i = 0; i < n; i++)
  {
    ssids[i] = WiFi.SSID(i);
  }
}

It's for an ESP8266 so you can ignore the "PROGMEM".

Except that, it might suggest that modifying the array was not an intention of the original program. Which might lead to some problems...

Bad idea Arduino/ESP8266WiFiScan.cpp at 076a4edf1e8146ef7420018a0b5b3eadc9acf6af · esp8266/Arduino · GitHub

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.