如果我在c语言中有这样的消息" .exe已停止工作"?

时间:2014-03-16 21:46:52

标签: c windows

我运行程序时遇到c语言问题 消息“x.exe已停止工作”。 例如,x是文件的名称。 我该如何解决?

enter code here

#include <stdio.h>
#include <stdlib.h>



struct student
{
int  id;
char *firs_name;
char *last_name;
char *email;
};

struct datetime
{
int date;
int  time;
};
typedef struct student student;
typedef struct datetime datetime;

struct submission
{
student *stud1;
student *stud2;
datetime *dt;
};

typedef struct submission submission;

int details(submission *data, int n1, datetime due_date, int *lates);
submission *return_clear_arr(submission *data,int n1);


submission *return_clear_arr(submission *data,int n1)
{
int i,j;

 for(i=0;i<n1;i++)
{
   for(j=1;j<n1;j++)
    {
    if(data[i].stud1->id==data[j].stud1->id && data[i].stud2->id==data  [j].stud2->id)
        {

            data[j].stud1->id=-1;
            data[j].stud2->id=-1;
        }
        if(data[i].stud1->id==data[j].stud1->id)
        {
          data[j].stud1->id=-1;
        }
        if(data[i].stud1->id==data[j].stud2->id)
        {
            data[j].stud2->id=-1;
        }
        if(data[i].stud2->id==data[j].stud1->id)
        {
         data[j].stud1->id=-1;
        }
        if(data[i].stud2->id==data[j].stud2->id)
        {
           data[j].stud2->id =-1;
        }
        else
        {
            continue;
        }
    }

}
return data;
}

int details(submission *data, int n1, datetime due_date, int *lates)
{
int i,counter=0,counter1=0;
submission *data2;
data2=return_clear_arr(data,n1);

for(i=0;i<n1;i++)
{
  if(data[i].dt->date < due_date.date)
  {
    if( data[i].stud1->id==-1 && data[i].stud2->id==-1)
    {
        continue;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id==-1)
    {
        counter++;
    }
    if( data[i].stud1->id==-1 && data[i].stud2->id!=-1)
    {
     counter++;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id!=-1)
    {
        counter+=2;
    }
  }
  if(data[i].dt->date > due_date.date)
  {
   if( data[i].stud1->id==-1 && data[i].stud2->id==-1)
    {
        continue;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id==-1)
    {
        counter1++;
    }
    if( data[i].stud1->id==-1 && data[i].stud2->id!=-1)
    {
     counter1++;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id!=-1)
    {
        counter1+=2;
    }
  }
  if(data[i].dt->date == due_date.date && data[i].dt->time < due_date.time)
  {
    if( data[i].stud1->id==-1 && data[i].stud2->id==-1)
    {
        continue;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id==-1)
    {
        counter++;
    }
    if( data[i].stud1->id==-1 && data[i].stud2->id!=-1)
    {
     counter++;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id!=-1)
    {
        counter+=2;
    }
  }
  if(data[i].dt->date == due_date.date && data[i].dt->time > due_date.time)
  {
   if( data[i].stud1->id==-1 && data[i].stud2->id==-1)
    {
        continue;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id==-1)
    {
        counter1++;
    }
    if( data[i].stud1->id==-1 && data[i].stud2->id!=-1)
    {
     counter1++;
    }
    if( data[i].stud1->id!=-1 && data[i].stud2->id!=-1)
    {
        counter1+=2;
    }
  }
}
*lates=counter1;
return counter;
}

int main()
{
int n1=6,result1;
int *lates1;
datetime due_date1={14314,1200};
submission data1[6];
submission sub1,sub2,sub3,sub4,sub5,sub6;
sub1.stud1[0].id= 23423446;
sub1.stud1[1].firs_name ="Yoram";
sub1.stud1[2].last_name= "Givon";
sub1.stud1[3].email= "yoram@gmail.com";
sub1.stud2[0].id= 823654434;
sub1.stud2[1].firs_name ="Mor";
sub1.stud2[2].last_name= "Kol";
sub1.stud2[3].email= "mor@gmail.com";
sub1.dt->date=13314;
sub1.dt->time=2000;
data1[0]=sub1;

sub2.stud1[0].id= 423423467;
sub2.stud1[1].firs_name ="Uri";
sub2.stud1[2].last_name= "Kaplan";
sub2.stud1[3].email= "uri@gmail.com";
sub2.stud2[0].id= 834654434;
sub2.stud2[1].firs_name ="Naama";
sub2.stud2[2].last_name= "Vilk";
sub2.stud2[3].email= "naama@gmail.com";
sub2.dt->date=12314;
sub2.dt->time=2000;
data1[1]=sub2;

sub3.stud1[0].id= 482634546;
sub3.stud1[1].firs_name ="Uriel";
sub3.stud1[2].last_name= "Shalom";
sub3.stud1[3].email= "uriel@gmail.com";
sub3.dt->date=14314;
sub3.dt->time=1100;
data1[2]=sub3;

sub4.stud1[0].id= 429283243;
sub4.stud1[1].firs_name ="Omer";
sub4.stud1[2].last_name= "Chen";
sub4.stud1[3].email= "omer@gmail.com";
sub4.stud2[0].id= 423654434;
sub4.stud2[1].firs_name ="Dan";
sub4.stud2[2].last_name= "Lerner";
sub4.stud2[3].email= "dan@gmail.com";
sub4.dt->date=13314;
sub4.dt->time=1000;
data1[3]=sub4;

sub5.stud1[0].id= 492381236;
sub5.stud1[1].firs_name ="Amit";
sub5.stud1[2].last_name= "Paz";
sub5.stud1[3].email= "amit@gmail.com";
sub5.dt->date=13314;
sub5.dt->time=800;
data1[4]=sub5;

sub6.stud1[0].id= 745919897;
sub6.stud1[1].firs_name ="Dikla";
sub6.stud1[2].last_name= "Gefen";
sub6.stud1[3].email= "dikla@gmail.com";
sub6.stud2[0].id= 925962312;
sub6.stud2[1].firs_name="Yaara";
sub6.stud2[2].last_name= "Shaked";
sub6.stud2[3].email= "yaara@gmail.com";
sub6.dt->date=14314;
sub6.dt->time=1159;
data1[5]=sub6;
result1=details(data1,n1,due_date1,lates1);
printf("%d",result1);
return 0;
}

嗨,我运行程序时遇到c语言问题 消息“x.exe已停止工作”。 例如,x是文件的名称。 我该如何解决?

1 个答案:

答案 0 :(得分:1)

您需要花时间精简如何使用IDE的调试器以及如何查看代码和任何编译器警告。你的问题出在main():

在这里,您创建了一堆提交结构实例:

submission sub1,sub2,sub3,sub4,sub5,sub6;

然后,您尝试执行此任务:

sub1.stud1[0].id= 23423446;

如果你看一下提交声明:

struct submission
{
student *stud1;
student *stud2;
datetime *dt;
};

你会看到stud1,是一个指针给学生,而不是学生。因此没有为结构分配任何内存。指针指向垃圾。然后,您尝试取消引用指针,并且blam,segfault。您需要将其初始化为学生类型的有效内存块。这留给你作为学习练习。