注册 登录
编程论坛 C++教室

给结构体的成员动态的赋值的问题

zhaozhiwei 发布于 2007-11-09 19:05, 641 次点击
只有本站会员才能查看附件,请 登录

我在做实验的时候需要给结构体内的字符数组动态的赋值
我测试用的的程序如下:
#include<iostream>
#include<cstring>
using namespace std;
typedef struct PCB
{
char Pname[10];
char Pstate;
int Psuper;
int ntime;
int rtime;
int atime;
struct PCB *next;
}PCB,*Link;
void main()
{
Link p;
char ch[10];
cin>>ch;
cout<<"*************************************"<<endl;
cout<<ch ;
strcpy(p->Pname ,ch);
cout<<p->Pname ;
}
提示的错误如图。不知道怎么修改?
2 回复
#2
frank2002007-11-09 19:23
#include<iostream>
#include<cstring>
using namespace std;
typedef struct PCB
{
char Pname[10];
char Pstate;
int Psuper;
int ntime;
int rtime;
int atime;
struct PCB *next;
}PCB,*Link;
void main()
{
Link p;
char ch[10];
cin>>ch;
cout<<"*************************************"<<endl;
cout<<ch ;
p = new PCB; //加上该句就可以了
strcpy(p->Pname ,ch);
cout<<p->Pname ;
}
#3
zhaozhiwei2007-11-09 23:56
嗯 谢谢
1