回 帖 发 新 帖 刷新版面

主题:[讨论]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);
}





回复列表 (共4个回复)

沙发

潜水的高手们啊,出手相救下吧,感激不尽。。。

板凳

所谓的“访问违例和内存不能为read”,一般要么是访问了空指针指向的对象,要么是访问了已经被销毁的对象,要么是数组越界。楼主可以从这些方面考虑。
长长的程序代码,楼主既不说明程序的主要功能,又不说明自己是输入了什么才导致的错误,这样即使有人想帮忙,也无从下手的。

3 楼

[quote] //我是用Dev-C++和MyTc来编译运行的 [/quote]

好象Dev-C++也是 gcc的内核吧!

我建议楼主您使用命令行来编译,有时候比IDE更方便。
在编译的时候加上 -Wall 这个命令,它会显示所有警告。有很多时候这些警告就是造成程序错误的原因。gcc -Wall 得到的结果是:
[quote] C:\Documents and Settings\Administrator\桌面\C>gcc -Wall -o test.exe test.c
test.c: In function `creat':
test.c:53: warning: char format, different type arg (arg 2)
test.c:72: warning: char format, different type arg (arg 2)
test.c: In function `insert':
test.c:142: warning: char format, different type arg (arg 2)
test.c:147: warning: suggest parentheses around assignment used as truth value
test.c: In function `del':
test.c:175: warning: suggest parentheses around assignment used as truth value
test.c:188: warning: suggest parentheses around assignment used as truth value
test.c: In function `main':
test.c:262: warning: control reaches end of non-void function  [/quote]



您看看原来的代码, 确实有很多问题,要改起来简单多了。根据这些警告楼主还是自己来改吧。

4 楼


我刚在vc6.0上运行,没有错啊!(当然要加上#include <stdlib.h> )

我来回复

您尚未登录,请登录后再回复。点此登录或注册