1
votes

I've already gone through the following links but couldn't get it to work:

Adding to c* invalid conversion from const char* to char*"

Why is conversion from string constant to 'char*' valid in C but invalid in C++

I'm working on arduino Uno. I need to send an argument of type char* to a function. I have the following code:

const char* topic = "SampleTopic";
const char* msg = "Hello";
publish(0, 0, 0, "msgid", topic, msg);

I'm getting this error:

initializing argument 5 of 'void GSM_MQTT::publish(char, char, char, unsigned int, char*, char*)'

void publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char *Topic, char *Message);

warning: invalid conversion from 'const char*' to 'char*' [-fpermissive]

publish(0, 0, 0, _generateMessageID(), topic, msg);

                                              ^

I've even tried using const std::string& topic = "SampleTopic"; but get this error:

'string' in namespace 'std' does not name a type

Even const char* topic = (char*)"SampleTopic"; and passing it as func(topic) gives the same error.

How can I resolve this ??

1
Strings and string literals is an area where C and C++ differs. You have to remember that C and C++ are two very distinct and different languages, with different semantic rules and constructs.Some programmer dude
As for your std::string error, Arduino may use C++ but IIRC it doesn't use the standard C++ library. Instead it have its own class-library, where you have for example a String class.Some programmer dude
Oh and the first error you get doesn't match the first code snippet, please try to create a minimal reproducible example to show us. And you should never convert a const anything to non-const. You could cast const away, but it's almost always a bad idea. The error message, by the way, contains a hint about why you get the warning (you pass a const char * to a function that expects a char *).Some programmer dude
If you are sure that publish does not modify the strings pointed to by Topic and Message, you could use an ugly cast to cast away the const: publish(0, 0, 0, (char *)topic, (char *)msg);. If publish is under your control, it would be better to change publish to use const char * parameters.Ian Abbott
@IanAbbott using (char*)"Topic Name" but it gives me ISO C++ forbids converting a string constant to 'char*'mrid

1 Answers

1
votes

I don't known anything about the GSM_MQTT::publish() function but if it requires some modifiable strings (char * without const) for Topic and Message, then you have to provide modifiable strings.

char topic[] = "SampleTopic";
char msg[] = "Hello";

(you have to make sure the storage is sufficient for the modifications)

On the other hand, if this function does not actually require some modifiable strings, then it is probably a mistake in the API and these parameters should have been declared const char *.
If this is actually the case, you can safely use const_cast (or even a traditional C cast) when passing your two const char * arguments.

const char* topic = "SampleTopic";
const char* msg = "Hello";
publish(0, 0, 0, ???msgid???, const_cast<char *>(topic), const_cast<char *>(msg));

Note that in your example you use the string "msgid" where an unsigned int is expected.