1

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");
}
  • 3
    No, it should not work this way. You are cofusing data types. char respresents a single character. So your first statement should be invalid. The second attempt looks reasonable but I'd use const char data[] instead. Now your string concatenation does not work at all because you are not using C++ Strings. Only those offer string concatenation with +. – Kwasmich Feb 5 '18 at 11:44
  • the easy way in arduino: Serial.print("this " + String(data) + " work"); – dandavis Feb 6 '18 at 0:20
4
const char data = "should";

In this case data is a single character, not a string. So it can store 's' but not "should".

Serial.print("this " + data + " work");

No, that will never work, even if you get your data types correct. You do not concatenate strings like that. All you are doing is attempting to add the addresses of two string literals to the address of a string constant. If that were possible (the compiler doesn't let you) you'll get gibberish (or a crash) as it tries to print out whatever is at that address.

Instead break it down:

Serial.print("this ");
Serial.print(should);
Serial.print(" work");
| improve this answer | |
  • 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. – Ruslan Feb 5 '18 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. – Majenko Feb 5 '18 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. – Ruslan Feb 5 '18 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. – Majenko Feb 5 '18 at 16:51
  • Operator overloading is a bad idea. This is one of them. How can a simple task (producing a string) need sam – user31481 Feb 5 '18 at 20:08
1

A char can only store one character.

and const char* data can store a pointer to a string.

You can copy it with strcpy:

const char* data = malloc(7);
if (data != 0)
{
    strcpy(data, "should");
}

This will create 7 bytes, which can store "should" (adding 1 byte extra for the \0 byte to denote the end of a string.

However, in your case you can create a const string like:

const char data[7]="should";

or

const char data[]="should";
| improve this answer | |
  • 2
    Tere is no space allocated that fits the string. :( – Kwasmich Feb 5 '18 at 11:45
  • 1
    Added also memory check – Michel Keijzers Feb 5 '18 at 11:47
  • Even now your code won't compile. And anyway, no point in using dynamic memory just to store a static string. – Ruslan Feb 5 '18 at 16:26
  • @Ruslan edited (this time compiled) – Michel Keijzers Feb 5 '18 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.