Cannot convert 'String' to 'char*' in initialization

hello there
this is code for controlling relay over internet but when i want to compile it show error

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>

WiFiClient espClient;
PubSubClient client(espClient);

aREST arestVar = aREST(client);

/*Enter an Unique ID to identify the device for arest.io
A Maximum of 6 characters are supported. Must be unique.*/

char* device_id = "re1403"; // Do not use this device_id. Create your own id. 

/* Enter SSID and Password of your 
WiFi Network*/
const char* ssid = "SSID"; // Name of WiFi Network. 
const char* password = "PASSWORD"; // Password of your WiFi Network.

void callback(char* topic, byte* payload, unsigned int length);

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

  client.setCallback(callback);
  
  // Give name and ID to device
  arestVar.set_id(device_id);
  arestVar.set_name("MyESP");

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print("*");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  char* out_topic = arestVar.get_topic();
}

void loop() 
{
  arestVar.loop(client);
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  arestVar.handle_callback(client, topic, payload, length);
}

and the error

In function 'void setup()':

test:42:39: error: cannot convert 'String' to 'char*' in initialization

   42 |   char* out_topic = arestVar.get_topic();

      |                     ~~~~~~~~~~~~~~~~~~^~

      |                                       |

      |                                       String

exit status 1

cannot convert 'String' to 'char*' in initialization


From aRest.h, the get_topic() function returns a String object, not a pointer to a char:

// Get topic
String get_topic() {
  return out_topic;
}
2 Likes

iam new can you explain more please how to solve this

Try:
char* out_topic = arestVar.get_topic().c_str();

".c_str()" is the 'member function' of String that returns the pointer to the String's internal character buffer so it can be treated as a C string (pointer to a null-terminated array of characters).

2 Likes

i tested it show this error after i change that
invalid conversion from 'const char*' to 'char*' [-fpermissive]

The line in question, even once you get it to compile, will do absolutely nothing! You create the new variable out_topic, then try to set out_topic to the value returned by arestVar.get_topic(), and then out_topic IMMEDIATELY goes out of scope and is discarded. So what is the point?

2 Likes

I found that code from here https://www.electronicshub.org/control-a-relay-from-anywhere-in-the-world-using-esp8266/

This doens't mean that is a good code...

char* out_topic is a local variable. Once the function setup() is executed it cease to exists.
It should be declared global and you can do after that

out_topic = (char *) arestVar.get_topic(). c_str(); 

But maybe is easiest for you declare as String

String out_topic;
....

 out_topic = arestVar.get_topic(); 

DO you khow out_topic is doing what ?
he is write set out put topic but i dont see where it is going?

2 Likes

With this code, absolute nothing!

3 Likes

Terrible idea, you shouldn't cast away const just to make the compiler happy, and you'll be left with a dangling pointer, referencing the storage of a temporary String that will be long gone by the time you want to use it.

So what is your idea about that
how to solve that

Store the (owning) String itself:

Looking at the aREST library at https://www.arduino.cc/reference/en/libraries/arest/ GitHub - marcoschwartz/aREST: A RESTful environment for Arduino aREST/aREST.h at master · marcoschwartz/aREST · GitHub

I think the easiest fix would be to change change:

to

void setup(void)
{
   ...
  // char* out_topic = arestVar.get_topic();
}

... per #6, #8, #9, #10.

If you actually need to the value elsewhere later, just call arestVar.get_topic() as needed.

or:

void setup(void)
{
   ...
  char* out_topic = arestVar.get_topic().c_str();
  // ACTUALLY DO SOMETHING WITH out_topic HERE
}
2 Likes

Won't work:

Well, you're going to have to explain that one. Did you not see the comment I put AFTER that line? Obviously, as I pointed out in post #6, that line, by itself is completely useless, as the variable goes out of scope immediately. But if the variable is used BEFORE it goes out of scope, it will work just fine. Also obviously, if the variable is simply defined in file or global scope, and only initialized in setup(), it should also work just fine.

This*:

char* out_topic = arestVar.get_topic().c_str();

is roughly equivalent to

char* out_topic;
{
  String tmp = arestVar.get_topic();
  out_topic = tmp.c_str();
} // tmp destroyed here
// use out_topic here

Temporaries (such as the return value of get_topic()) are destroyed after completion of the expression they're part of.

(*) Ignoring the fact that this doesn't compile because it's not const-correct.

You are CLEARLY not reading my posts before responding to them. I POINTED EXACTLY THAT OUT in my first post, and repeated it in my last post. And made it clear in the comment in my code ''snippet", which I also pointed out, that the value of out-topic MUST BE USED BEFORE setup() is exited. Please READ before responding.

Your attitude is totally uncalled for.

Please read my previous post, the temporary is destroyed at the end of the expression, i.e. the end of the line. I didn't mention the end of the setup function at all.