为什么出错了?什么原因??

qqpass2008 2007-04-18 01:06:07
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct stu{
int num;
char name[20];
char sex;
float score;
}student[2]={{050101,"许元元",'w',502},{050102,"张帆",'m',498}};

typedef struct node{
struct stu data;
struct node *next;
}Llist,*linklist;

createList(linklist p,struct stu student[],int n)
{linklist h=NULL;
int i;Llist *s,*r;
for(i=0;i<n;i++)
{s=(Llist *)malloc(sizeof(Llist));
s->data.num=student[i].num;
strcpy(s->data.name,student[i].name);
s->data.sex=student[i].sex;
s->data.score=student[i].score;
s->next=NULL;
if(h==NULL){h=s;r=s;}
else{r->next=s;
r=s;}
}
}
main()
{Llist *a1=NULL,*p;
createList(&a1,student,2);
p=a1;
while(p)
{printf("\n%d\t%s\t%c\t%f",p->data.num,p->data.name,p->data.sex,p->data.score);
p=p->next;
}

}
...全文
167 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqpass2008 2007-04-18
  • 打赏
  • 举报
回复
完全明白了,谢谢!!
freshui 2007-04-18
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct stu{
int num;
char name[20];
char sex;
float score;
}student[2]={{050101,"许元元",'w',502},{050102,"张帆",'m',498}};

typedef struct node{
struct stu data;
struct node *next;
}Llist,*linklist;

void createList(linklist &p,struct stu student[],int n)
{
linklist h=NULL;
int i;Llist *s,*r;

for(i=0;i<n;i++)
{
s=(Llist *)malloc(sizeof(Llist));
s->data.num=student[i].num;
strcpy(s->data.name,student[i].name);
s->data.sex=student[i].sex;
s->data.score=student[i].score;
s->next=NULL;
if(h==NULL)
{
h=s;r=s;
}
else{
r->next=s;
r=s;
}
}

p=h;
}
main()
{ Llist *a1=NULL,*p;
createList(a1,student,2);
p=a1;
while(p)
{printf("\n%d\t%s\t%c\t%f",p->data.num,p->data.name,p->data.sex,p->data.score);
p=p->next;
}

}
freshui 2007-04-18
  • 打赏
  • 举报
回复
你要显示的结果是啥?
那显然后面那个函数最后一句是
p=h;

这样是指向list的head, 才能全部显示
qqpass2008 2007-04-18
  • 打赏
  • 举报
回复
显示不正确,什么原因??
freshui 2007-04-18
  • 打赏
  • 举报
回复
完整的:

#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct stu{
int num;
char name[20];
char sex;
float score;
}student[2]={{050101,"许元元",'w',502},{050102,"张帆",'m',498}};

typedef struct node{
struct stu data;
struct node *next;
}Llist,*linklist;

void createList(linklist &p,struct stu student[],int n)
{
linklist h=NULL;
int i;Llist *s,*r;
for(i=0;i<n;i++)
{s=(Llist *)malloc(sizeof(Llist));
s->data.num=student[i].num;
strcpy(s->data.name,student[i].name);
s->data.sex=student[i].sex;
s->data.score=student[i].score;
s->next=NULL;
if(h==NULL){h=s;r=s;}
else{r->next=s;
r=s;}
}
p=s;
}
main()
{ Llist *a1=NULL,*p;
createList(a1,student,2);
p=a1;
while(p)
{printf("\n%d\t%s\t%c\t%f",p->data.num,p->data.name,p->data.sex,p->data.score);
p=p->next;
}

}
freshui 2007-04-18
  • 打赏
  • 举报
回复
void createList(linklist *p,struct stu student[],int n)
改成
void createList(linklist &p,struct stu student[],int n)
并在这个函数的最后一个}前加上:
p=r;
即可
qqpass2008 2007-04-18
  • 打赏
  • 举报
回复
改啦,但怎么还是没有显示出数据来???
freshui 2007-04-18
  • 打赏
  • 举报
回复
void createList(linklist *p,struct stu student[],int n)

你在函数中用到p了没?
freshui 2007-04-18
  • 打赏
  • 举报
回复
1, createList()前面应该加个void

2, 你函数的原型是: void createList(linklist,struct stu *,int)

你调用时:createList(&a1,student,2);
变成了:void createList(linklist *,struct stu *,int)

本身已经是指针了, 再取地址干吗?
qqpass2008 2007-04-18
  • 打赏
  • 举报
回复
运行后什么都没显示啊!!
mochen5460 2007-04-18
  • 打赏
  • 举报
回复
createList(a1,student,2);
p=a1;
参数类型不对,改成上面的试试
qqpass2008 2007-04-18
  • 打赏
  • 举报
回复
这个什么意思??

E:\Project\temp.cpp(33) : error C2664: 'createList' : cannot convert parameter 1 from 'struct node ** ' to 'struct node *'

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧