1

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 ??

Community
  • 1
  • 1
mrid
  • 5,782
  • 5
  • 28
  • 71
  • 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 Jul 29 '19 at 17:23
  • 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`](https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/) class. – Some programmer dude Jul 29 '19 at 17:25
  • Oh and the first error you get doesn't match the first code snippet, please try to create a [mcve] 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 Jul 29 '19 at 17:27
  • 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 Jul 29 '19 at 17:28
  • @IanAbbott using `(char*)"Topic Name"` but it gives me `ISO C++ forbids converting a string constant to 'char*'` – mrid Jul 29 '19 at 17:37

1 Answers1

1

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.

prog-fh
  • 13,492
  • 1
  • 15
  • 30
  • According to the library code these parameters are not modified inside and could be declared const(and it should be if it should be const correct). – KIIV Jul 29 '19 at 21:46