+ 1

Why it is giving error while excessing ....

Why it is giving error while excessing string as it is public in class and also it giving warning for string when it is private . https://code.sololearn.com/cXLxVY3RWW7j/?ref=app

24th May 2022, 7:14 AM
Abhay mishra
Abhay mishra - avatar
1 Answer
+ 3
Incompatible types. To begin with, human.nickname[20] is the 21st character in a character array. You cannot put a const string into a char spot. If you remove the array indexing operation, human.nickname = "BABU", then you are assigning to an array. The declaration public : char nickname[20] ; reserves 20 chars inside the memory area of the object for a string. "BABU" is a string in the read only data section of your program. You cannot copy on assign in C++. You have to manually copy char for char into the object. Alternatively, you make nickname point to a const char instead public : const char *nickname; But best would be to use a string datatype, instead of dealing with C strings.
24th May 2022, 8:51 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar