Why it is giving error while excessing .... | Sololearn: Learn to code for FREE!


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

5/24/2022 7:14:02 AM

Abhay mishra

1 Answer

New Answer


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.