高手来帮小弟看一个小程序,编译通过了,运行时总是内存报错

来源:百度知道 编辑:UC知道 时间:2024/09/22 15:38:04
class employment
{
char *position;
int num;
char *name;
public:
void input(char*p,int n,char*na)
{
strcpy(position,p);
strcpy(name,na);
num=n;
}
void disp()
{
cout<<position<<" "<<num<<" "<<name;
}
};

void main()
{
employment e;
char *p,*na;
char t,m;
int n;
cout<<"please ernter the name:";
cin>>t;
cout<<"enter the number:";
cin>>n;
cout<<"enter the position:";
cin>>m;
p=&t;
na=&m;
e.input(p,n,na);
e.disp();

}
哪里错了啊?

name,position不能用char *,要分配一个足够大的数组,比如
char name[80];

类中的char *position和char *name,应该在构造函数中为其分配内存空间,不然当然会内存报错