+ 1

C++ Error

Can someone help? The following code outputs an error and I'm not sure how to fix it. char arr [1] = {}; arr [0] = char "A";

15th Mar 2018, 8:45 PM
Zack
Zack - avatar
7 odpowiedzi
+ 4
Char is ' ' not " ". And, I’m not sure, but a char is not an array, because there’s only one character
15th Mar 2018, 8:47 PM
Chalza
Chalza - avatar
+ 5
Because "A" is a char array with two elements and 'A' is a single char. char name[] = "Zack"; and char name[] = {'Z', 'a', 'c', 'k', '\0'} are the same 5 character arrays.
15th Mar 2018, 9:11 PM
John Wells
John Wells - avatar
+ 5
Because that character is used to terminate "strings". Otherwise, they would never end and would print all of memory.
15th Mar 2018, 10:43 PM
John Wells
John Wells - avatar
+ 5
char x[] = "Zack"; while (*x != '\0') { cout << *x; x++; } is identical in concept to: char x[] = "Zack"; cout << x; The second is faster as the loop is within the function cout uses instead of multiple calls to the function.
15th Mar 2018, 10:49 PM
John Wells
John Wells - avatar
+ 3
Why '\0' at the end ?
15th Mar 2018, 9:36 PM
Chalza
Chalza - avatar
+ 3
Ok thanks
16th Mar 2018, 7:07 AM
Chalza
Chalza - avatar
+ 1
yes it was ' '. I also had to remove the second "char" for it to work. But why should ' ' and " " be different?
15th Mar 2018, 9:01 PM
Zack
Zack - avatar