#include <iostream>
using namespace std ;
struct Person
{
char *name ;
int age ;
char sex ;
};
void main()
{
Person man[2] ;
int i ;
for(i=0; i<2; i++)
{
man[i].name = new char[5] ; //man[i].name = new char ;
cout << "请输入姓名:" ;
cin >> man[i].name ;
cout << "请输入年龄:" ;
cin >> man[i].age ;
cout << "请输入性别:" ;
cin >> man[i].sex ;
cout << endl ;
}
for(i=0; i<2; i++)
{
cout << "姓名:" << man[i].name ;
cout << ",年龄:" << man[i].age ;
cout << ",性别:" << man[i].sex ;
cout << endl ;
delete [] man[i].name ; //delete man[i].name ;
}
}
为什么像注释那样修改程序后,程序会出错呢??当我像注释那样修改后,再注释掉cin >> man[i].name ;一句,程序又正确了,这又是为什么呢??
麻烦高手解释一下。感谢了!!