请高手们看看哪里出错了
# include <stdio.h>
# include <malloc.h>
# define LEN sizeof(struct Student)
struct Student
{
long num;
float score;
struct Student *next;
};
int n;
struct Student * creat()
{
struct student * head;
struct Student *p1, *p2;
n = 0;
p1=p2=( struct Student * )malloc(LEN);
sanf("%ld, %f", &p1->num, &p1->score);
head=NULL;
while(p1 -> num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student * )malloc(LEN);
scanf("%ld, %f", &p1->num, &p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct Student head)
{
struct Student * p;
printf("\nNow,These %d records are:\n", n);
p=head;
if(head!==NULL)
do
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
while(p!=NULL);
}
void main()
{
struct Student * head;
head=creat();
print(head);
}
--------------------Configuration: xx - Win32 Debug--------------------
Compiling...
xx.cpp
C:\Documents and Settings\Administrator\桌面\xx.cpp(20) : error C2065: 'sanf' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\xx.cpp(27) : error C2440: '=' : cannot convert from 'struct Student *' to 'struct creat::student *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Administrator\桌面\xx.cpp(36) : error C2440: 'return' : cannot convert from 'struct creat::student *' to 'struct Student *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Documents and Settings\Administrator\桌面\xx.cpp(43) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct Student' (or there is no acceptable conversion)
C:\Documents and Settings\Administrator\桌面\xx.cpp(44) : error C2059: syntax error : '='
C:\Documents and Settings\Administrator\桌面\xx.cpp(54) : error C2664: 'print' : cannot convert parameter 1 from 'struct Student *' to 'struct Student'
No constructor could take the source type, or constructor overload resolution was ambiguous
执行 cl.exe 时出错.
xx.exe - 1 error(s), 0 warning(s)
[解决办法]
# include <stdio.h># include <malloc.h># define LEN sizeof(struct Student)struct Student{ long num; float score; struct Student *next;};int n;struct Student * creat(){ struct Student * head; struct Student *p1, *p2; n = 0; p1=p2=( struct Student * )malloc(LEN); scanf("%ld,%f", &p1->num, &p1->score); head=NULL; while(p1 -> num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Student * )malloc(LEN); scanf("%ld,%f",&p1->num,&p1->score); } p2->next=NULL; return(head); }void print(struct Student *head){ struct Student *p; printf("\nNow,These %d records are:\n", n); p=head; if(head!=NULL) do { printf("%ld %5.1f\n",p->num,p->score); p=p->next; } while(p!=NULL);}void main(){ struct Student * head; head=creat(); print(head);}