Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Why I'm getting this error: invalid conversion from 'const char*' to 'char' [-fpermissive]?

Why I'm getting this error?

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

Here is my simple sketch:

const char data = "should";
//I have also tried:
//const char* data = "should";
void setup() {
 Serial.begin(9600);
}
void loop() {
 Serial.print("this " + data + " work");
}

Answer*

Draft saved
Draft discarded
Cancel
11
  • You'll get gibberish as it tries to print out whatever is at that address. — no, you'll get compilation error. You can subtract pointers in C++, but not add. Commented Feb 5, 2018 at 16:32
  • @Ruslan Actually, in that specific instance you get a compilation error because the operands differ between a const char * for the variable and a const char[6] for the first literal. If you cast the literals as const char * you can subtract one and only one, otherwise you get differing arguments between the results of one subtraction (int) and the other string literal. The point I am making above though is that you do not concatenate strings by using +. Whether the syntax is "right" for something that is completely wrong anyway is neither here nor there. Commented Feb 5, 2018 at 16:42
  • You don't have to cast: the array will decay to a pointer before any arithmetic. But yes, the second subtraction would indeed cause an error because it'd try to subtract const char* from ptrdiff_t. Commented Feb 5, 2018 at 16:48
  • Ah yes, when subtracting it decays fine (as long as you only have one bit of arithmetic, otherwise you have to cast the result). When trying to (wrongly) add it doesn't decay, it just complains. Commented Feb 5, 2018 at 16:51
  • Operator overloading is a bad idea. This is one of them. How can a simple task (producing a string) need sam Commented Feb 5, 2018 at 20:08

AltStyle によって変換されたページ (->オリジナル) /