📄 subject_16539.htm
字号:
<p>
序号:16539 发表者:木子 发表日期:2002-09-29 12:58:40
<br>主题:为什么对指针name赋不了值?
<br>内容:#include "iostream.h"<BR>struct student<BR>{<BR> char *name;<BR> float score;<BR> int student_id;<BR> student *next;<BR>};<BR> student *creat(void) <BR>{<BR> //定义一个头指针作为链表头<BR> int n;<BR> student *head; <BR> student *p1,*p2;<BR> n=0; <BR> //用new动态的分配内存<BR> p1=p2=new(student); <BR> cin>>p1->name>>p1->student_id>>p1->score;<BR> head=NULL;<BR> while(p1->student_id!=0)<BR> {<BR> n=n+1;<BR> if(n==1)head=p1;<BR> else p2->next=p1;<BR> p2=p1;<BR> p1=new(student);<BR> cin>>p1->name>>p1->student_id>>p1->score;<BR> }<BR> p2->next=NULL;<BR> return(head);<BR>}<BR> void main()<BR> {<BR> creat();<BR> }
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:CDMA2000 回复日期:2002-09-29 13:11:40
<br>内容:结构里的char *name;这样定义有问题!<BR>你用new(student)时<BR>并没有为name分配内存!<BR>你最好是这样定义:<BR>struct student<BR>{<BR> char name[16];<BR> float score;<BR> int student_id;<BR> student *next;<BR>};<BR><BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -