C语言怎么就是运行不了呐???

来源:百度知道 编辑:UC知道 时间:2024/05/25 22:53:35
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{ char name[20];
char addr[50];
long tel;
long post_number;
struct student *next;
};
int n;
struct student *creat(void)
{ struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%s,%s,%ld,%ld",&p1->name,&p1->addr,&p1->tel,&p1->post_number);
head=NULL;
while(strcmp(p1->name,"no")!=0)
{ n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s,%s,%ld,%ld",&p1->name,&p1->addr,&p1->tel,&p1->post_number);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{ struct student *p;
printf("

&p1->name,&p1->addr
这二个是字符数组,p1->name这个是数组名,代表的是起始地址,输入的时候,不能加&,这二个都是这样的。对你的程序我给你小小的调整了一下,都是在这二处,然后就好用了,程序如下:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{ char name[20];
char addr[50];
long tel;
long post_number;
struct student *next;
};
int n;
struct student *creat(void)
{ struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
puts("input name:");
scanf("%s",p1->name);
puts("input addr:");
scanf("%s",p1->addr);
puts("input tel");
scanf("%ld",&p1->tel);
puts("input post_number:");
scanf("%ld",&p1->post_number);
//scanf("%s,%s,%ld,%ld",&p1->name,&p1->addr,&p1->tel,&p1->post_number)