Hello, guys!
I am confused with initialization of char[] array with string. I've not yet found correct answer for the question. Please help!
The below code contains two lines of code (b initialization). One works correctly (if char pointer used), but another one don't (if char[] used) .
class C {
char a[6] = "Hello";
char b[] = " World!"; // Does not work! WHY???
// char *b = " World!"; // Do work
char c = 'x';
char d = 'y';
public:
void print() {
Serial.println(a);
Serial.println(b);
Serial.println(c);
Serial.println(d);
Serial.println("");
Serial.println(int(a));
Serial.println(int(b));
Serial.println(int(&c));
Serial.println(int(&d));
}
};
C c;
void setup() {
Serial.begin(9600);
while (!Serial) { }
c.print();
}
void loop() {
}
I expected:
Hello
World!
x
y
256
285
264
265
But I see:
Hello
xy
x
y
256
262
262
263
In case of (char b[] = " World!") char pointer is incorrect. It does not point to " World!" string. More over the pointer of the char c declared below has the same value as b.