class Person
{
private:
char name[20];
char sex;
int age;
char* addr;
public:
Person()
{
strcpy_s(name, "张三");
sex = 'x';
age = 0;
addr = new char[20];
addr[0] = '\0';
}
Person(char* n, int age, char sex, char* addr)
{
strcpy_s(this->name, strlen(n) + 1, n);
this->age = age;
this->sex = sex;
this->addr = new char[strlen(addr) + 1];
strcpy_s(this->addr, strlen(addr) + 1, addr);
}
Person(Person& p)
{
strcpy_s(this->name, strlen(name + 1), p.name);
this->age = p.age;
this->sex = p.sex;
addr = new char[strlen(p.addr) + 1];
strcpy_s(this->addr, strlen(p.addr) + 1, p.addr);
}
~Person()
{
delete[] addr;
}
void ShowMe();
};
void Person::ShowMe()
{
cout << "姓名为:" << name << ",年龄为:" << age << ",性别为:" << sex << ",地址为:" << addr << endl;
}
int main()
{
Person person1;
person1.ShowMe();
Person person2((char*)"小丽", 19, 'm', (char*)"中国广东省深圳市");
person2.ShowMe();
}
麻烦帮忙解释一下addr[0] = '\0'; 这个句子
为什么把字符串结束标识符放到了第一个字节里面?而不是字符串的最后一个字节里面?