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");
}
2 Answers 2
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");
-
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– Ruslan2018年02月05日 16:32:02 +00:00Commented 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 aconst char[6]
for the first literal. If you cast the literals asconst 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– Majenko2018年02月05日 16:42:18 +00:00Commented 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*
fromptrdiff_t
.Ruslan– Ruslan2018年02月05日 16:48:27 +00:00Commented 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.Majenko– Majenko2018年02月05日 16:51:46 +00:00Commented 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 samuser31481– user314812018年02月05日 20:08:47 +00:00Commented Feb 5, 2018 at 20:08
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";
-
2Tere is no space allocated that fits the string. :(Kwasmich– Kwasmich2018年02月05日 11:45:49 +00:00Commented Feb 5, 2018 at 11:45
-
1Added also memory checkMichel Keijzers– Michel Keijzers2018年02月05日 11:47:19 +00:00Commented 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.Ruslan– Ruslan2018年02月05日 16:26:41 +00:00Commented Feb 5, 2018 at 16:26
-
@Ruslan edited (this time compiled)Michel Keijzers– Michel Keijzers2018年02月05日 19:43:54 +00:00Commented Feb 5, 2018 at 19:43
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+
.Serial.print("this " + String(data) + " work");