主题:[讨论]c程序编译可以通过,执行时报错:访问违例和内存不能为read
//我是用Dev-C++和MyTc来编译运行的。系统环境是XP SP3。执行创建通讯录以后的其他函数报错。。。调试过程中,一般都是如下红色标记的语句哪里报错,由于对指针不太感冒,不知如何修改了[tk20]
#include<stdio.h>
#include<malloc.h>
struct student {
int num;
char name[20];
char phone[15];
int age;
char addr[20];
char Email[20];
struct student *next;
};
int n;
struct student *head;
void welcom()
{printf("\n\n\n\n\n");
printf(" ******************************************************************\n");
printf(" *");printf(" 欢迎使用通讯录管理系统 *\n\n");
printf(" ******************************************************************\n");
printf("\n\n 进入主菜单(1)\n");
printf(" 退出(0)\n");
}
void menu()
{
printf("\n\n\n --------------------------------------------------------------------\n");
printf(" "); printf("\t\t 个人通讯录"); printf("\t\t");
printf("\n --------------------------------------------------------------------- \n");
printf(" "); printf("\t 1.创建通讯录"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 2.删除信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 3.插入信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 4.显示信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 5.保存信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 6.读取数据"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 7. 排序 "); printf("\t\t\t\t\n\n");
printf(" ---------------------------------------------------------------------\n");
printf(" 输入你的选择1-7 : \n\n\n");
}
struct student *creat()
{
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("学号: ");
scanf("%d",&p1->num);
printf("\n姓名: ");
scanf("%s",p1->name);
printf("\n联系方式: ");
scanf("%s",&p1->phone);
printf("\n电子邮件: ");
scanf("%s",p1->Email);
printf("\n住址: ");
scanf("%s",p1->addr);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else
{
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("学号: ");
scanf("%d",&p1->num);
printf("\n姓名: ");
scanf("%s",p1->name);
printf("\n联系方式: ");
scanf("%s",&p1->phone);
printf("\n电子邮件: ");
scanf("%s",p1->Email);
printf("\n住址: ");
scanf("%s",p1->addr);
}
}
return (head);
}
void print(struct student *head)
{struct student *p,*q;
p=head;
printf("学号 姓名 联系方式 电子邮件 住址 \n");
if(head!=NULL)
do{
printf("%-10d%-10s%-15s%-20s%-10s\n",p->num,p->name,p->phone,p->Email,p->addr);
q=p;
p=q->next;
}while(p!=NULL);
}
struct student *sort(struct student *head)
{
struct student *h1,*p,*p1,*p2,*p3;
h1=NULL;
h1->next=head;
p1=p2=head;
p3=p2->next;
[color=#FF0000] while((p3->num>=p2->num)&&(p3->next!=NULL));[/color]
{
p2=p3;
p3=p3->next;
}
while(p3->num<p2->num)
{
p=h1;
p1=head;
while(p1->num<p3->num)
{
p=p1;
p1=p1->next;
}
p->next=p3;
p2->next=p3->next;
p3->next=p1;
p3=p2->next;
}
printf("已排序完毕");
return h1;
}
struct student *insert(struct student *head)
{
struct student *p,*p1,*p2;
p1=head;
p=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的数据\n");
printf("学号: ");
scanf("%d",&p->num);
printf("\n姓名: ");
scanf("%s",p->name);
printf("\n联系方式: ");
scanf("%s",&p->phone);
printf("\n电子邮件: ");
scanf("%s",p->Email);
printf("\n住址: ");
scanf("%s",p->addr);
if(head=NULL)
{
head=p;
p->next=NULL;
}
else
{ while((p->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p2->next;}
if(p->num<=p1->num)
{if(head==p1)
head=p;
else
p2->next=p;
p->next=p1;}
else{p1->next=p;
p->next=NULL;}
}
n=n+1;
return(head);
}
struct student *del(struct student *head)
{
int x;
struct student *p,*p1;
printf("请输入要删除的学号\n");
scanf("%d",&x);
[color=#FF0000] if(head=NULL)[/color]
{
printf("这是空表");
return head;
}
p1=head;
[color=#FF0000] while((x!=p1->num)&&(p1->next!=NULL))[/color]
{
p=p1;
p1=p1->next;
}
if(x==p1->num)
{
if(p1=head)
head=p1->next;
else
p->next=p1->next;
free(p1);
printf("已删除节点%d\n",x);
}
else
printf("没找到节点x\n");
return head;
}
void save(struct student *head)
{FILE *fp;
struct student *p;
if((fp=fopen("stu_list","wb"))==NULL)
{printf("文件不存在\n");
return;
}
for(p=head;p!=NULL;p=p->next)
if(fwrite(p,sizeof(struct student),1,fp)!=1)
printf("456\n");
fclose(fp);
}[code][/code]
void load()
{FILE *fp;
struct student *p;
fp=fopen("stu_list","rb");
printf("学号 姓名 联系方式 电子邮件 住址 \n");
for(p=(struct student *)fp;p!=NULL;p=p->next)
{
fread(p,sizeof(struct student),1,fp);
printf("%-10d%-10s%-15s%-20s%-10s\n",p->num,p->name,p->phone,p->Email,p->addr);
}
fclose (fp);
}
int main()
{
struct student *h;
int a,b;
h=NULL;
welcom();
scanf("%d",&b);
if(b==0)
exit(0);
else
do
{
menu();
scanf("%d",&a);
switch(a)
{
case 1:h=creat();break;
case 2:h=del(h);break;
case 3:h=insert(h);break;
case 4:print(h);break;
case 5:save(h);break;
case 6:load();break;
case 7:h=sort(h);break;
}
}while(a!=0);
}
#include<stdio.h>
#include<malloc.h>
struct student {
int num;
char name[20];
char phone[15];
int age;
char addr[20];
char Email[20];
struct student *next;
};
int n;
struct student *head;
void welcom()
{printf("\n\n\n\n\n");
printf(" ******************************************************************\n");
printf(" *");printf(" 欢迎使用通讯录管理系统 *\n\n");
printf(" ******************************************************************\n");
printf("\n\n 进入主菜单(1)\n");
printf(" 退出(0)\n");
}
void menu()
{
printf("\n\n\n --------------------------------------------------------------------\n");
printf(" "); printf("\t\t 个人通讯录"); printf("\t\t");
printf("\n --------------------------------------------------------------------- \n");
printf(" "); printf("\t 1.创建通讯录"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 2.删除信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 3.插入信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 4.显示信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 5.保存信息"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 6.读取数据"); printf("\t\t\t\t\n\n");
printf(" "); printf("\t 7. 排序 "); printf("\t\t\t\t\n\n");
printf(" ---------------------------------------------------------------------\n");
printf(" 输入你的选择1-7 : \n\n\n");
}
struct student *creat()
{
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("学号: ");
scanf("%d",&p1->num);
printf("\n姓名: ");
scanf("%s",p1->name);
printf("\n联系方式: ");
scanf("%s",&p1->phone);
printf("\n电子邮件: ");
scanf("%s",p1->Email);
printf("\n住址: ");
scanf("%s",p1->addr);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else
{
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
printf("学号: ");
scanf("%d",&p1->num);
printf("\n姓名: ");
scanf("%s",p1->name);
printf("\n联系方式: ");
scanf("%s",&p1->phone);
printf("\n电子邮件: ");
scanf("%s",p1->Email);
printf("\n住址: ");
scanf("%s",p1->addr);
}
}
return (head);
}
void print(struct student *head)
{struct student *p,*q;
p=head;
printf("学号 姓名 联系方式 电子邮件 住址 \n");
if(head!=NULL)
do{
printf("%-10d%-10s%-15s%-20s%-10s\n",p->num,p->name,p->phone,p->Email,p->addr);
q=p;
p=q->next;
}while(p!=NULL);
}
struct student *sort(struct student *head)
{
struct student *h1,*p,*p1,*p2,*p3;
h1=NULL;
h1->next=head;
p1=p2=head;
p3=p2->next;
[color=#FF0000] while((p3->num>=p2->num)&&(p3->next!=NULL));[/color]
{
p2=p3;
p3=p3->next;
}
while(p3->num<p2->num)
{
p=h1;
p1=head;
while(p1->num<p3->num)
{
p=p1;
p1=p1->next;
}
p->next=p3;
p2->next=p3->next;
p3->next=p1;
p3=p2->next;
}
printf("已排序完毕");
return h1;
}
struct student *insert(struct student *head)
{
struct student *p,*p1,*p2;
p1=head;
p=(struct student *)malloc(sizeof(struct student));
printf("请输入要插入的数据\n");
printf("学号: ");
scanf("%d",&p->num);
printf("\n姓名: ");
scanf("%s",p->name);
printf("\n联系方式: ");
scanf("%s",&p->phone);
printf("\n电子邮件: ");
scanf("%s",p->Email);
printf("\n住址: ");
scanf("%s",p->addr);
if(head=NULL)
{
head=p;
p->next=NULL;
}
else
{ while((p->num>p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p2->next;}
if(p->num<=p1->num)
{if(head==p1)
head=p;
else
p2->next=p;
p->next=p1;}
else{p1->next=p;
p->next=NULL;}
}
n=n+1;
return(head);
}
struct student *del(struct student *head)
{
int x;
struct student *p,*p1;
printf("请输入要删除的学号\n");
scanf("%d",&x);
[color=#FF0000] if(head=NULL)[/color]
{
printf("这是空表");
return head;
}
p1=head;
[color=#FF0000] while((x!=p1->num)&&(p1->next!=NULL))[/color]
{
p=p1;
p1=p1->next;
}
if(x==p1->num)
{
if(p1=head)
head=p1->next;
else
p->next=p1->next;
free(p1);
printf("已删除节点%d\n",x);
}
else
printf("没找到节点x\n");
return head;
}
void save(struct student *head)
{FILE *fp;
struct student *p;
if((fp=fopen("stu_list","wb"))==NULL)
{printf("文件不存在\n");
return;
}
for(p=head;p!=NULL;p=p->next)
if(fwrite(p,sizeof(struct student),1,fp)!=1)
printf("456\n");
fclose(fp);
}[code][/code]
void load()
{FILE *fp;
struct student *p;
fp=fopen("stu_list","rb");
printf("学号 姓名 联系方式 电子邮件 住址 \n");
for(p=(struct student *)fp;p!=NULL;p=p->next)
{
fread(p,sizeof(struct student),1,fp);
printf("%-10d%-10s%-15s%-20s%-10s\n",p->num,p->name,p->phone,p->Email,p->addr);
}
fclose (fp);
}
int main()
{
struct student *h;
int a,b;
h=NULL;
welcom();
scanf("%d",&b);
if(b==0)
exit(0);
else
do
{
menu();
scanf("%d",&a);
switch(a)
{
case 1:h=creat();break;
case 2:h=del(h);break;
case 3:h=insert(h);break;
case 4:print(h);break;
case 5:save(h);break;
case 6:load();break;
case 7:h=sort(h);break;
}
}while(a!=0);
}