读书人

关于指针追加的有关问题 急 .

发布时间: 2012-02-20 21:18:25 作者: rapoo

关于指针追加的问题 急 ...
结构如下
struct stud
{
char number[20];
char name[10];
char clas[10];
char math[10];
char chinese[10];
char english[10];
stud * next;
};
stud *ps1;
stud *hd;
我想在指针结尾再添加一条记录
hd=head;
while(hd) //把指针走到最后
ps1=new stud;
for(int i=0;i <10;i++)
{
ps1-> number[i]=string[i];
ps1-> name[i]=nm[i];
ps1-> clas[i]=cs[i];
ps1-> math[i]=ma[i];
ps1-> chinese[i]=ch[i];
ps1-> english[i]=en[i];
}
hd-> next=ps1;
hd=ps1;
hd-> next=NULL;
编译都没错但执行后 报 程序遇到问题需要关闭

我想问下 如何 在指针的最后(hd-> next==NULl)追加新的内容使指针(hd-> next=p1) ,ps1-> next=NUll


[解决办法]
老大:while(hd) //把指针走到最后 hd = hd-> next; 你这样最后hd是NULL的,还怎么能hd-> next=ps1;再指向新结点ps1????

while(hd-> next) //把指针走到最后
hd = hd-> next;

读书人网 >C语言

热点推荐