2

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");
}
VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Feb 5, 2018 at 11:40
2
  • 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 +. Commented Feb 5, 2018 at 11:44
  • the easy way in arduino: Serial.print("this " + String(data) + " work"); Commented Feb 6, 2018 at 0:20

2 Answers 2

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");
answered Feb 5, 2018 at 11:55
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
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";
answered Feb 5, 2018 at 11:44
4
  • 2
    Tere is no space allocated that fits the string. :( Commented Feb 5, 2018 at 11:45
  • 1
    Added also memory check Commented Feb 5, 2018 at 11:47
  • Even now your code won't compile. And anyway, no point in using dynamic memory just to store a static string. Commented Feb 5, 2018 at 16:26
  • @Ruslan edited (this time compiled) Commented Feb 5, 2018 at 19:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.