【问题标题】:When trying to print a linked list nodes the program is turning off尝试打印链接列表节点时,程序正在关闭
【发布时间】:2023-01-05 22:14:31
【问题描述】:

我正在使用单链表编写电话簿代码。当我尝试打印联系人时,它们被打印出来,但随后程序关闭。

这是节点结构:

struct contact{
  char *number;
  char *name;
  struct contact* next;
};
typedef struct contact contact;

这是打印联系人功能:

void list(){
    contact *temp = first;
    if(first==NULL){
      printf("list is empty\n");
    }
    while(temp!=NULL){
      printf("name: %s  number: %s\n", temp->name, temp->number);
      temp = temp->next;
      
    }
    getch();
    system("cls");
  menu();
}

菜单()功能:

void menu(){
       system("cls");
  printf("+++++++++++++++++++Phone book+++++++++++++++++++++++++\n     1.add.\n     2.delete.\n     3.search.\n     4.modify.\n     5.list contacts.\n     6.recent\n     7.exit\n ");
  switch(getch())
  {
    case '1':
        add();
        break;
    case '2':
        delete();
        break;
    case '3':
        search();
        break;
    case '4':
        modify();
        break;
    case '5':
        list();
        break;
    case '6':
        recent();
        break;
    case '7':
        exit(0);
        break;
    default: system("cls");
                printf("\nPress any key");
                getch();
menu();
}
}

编辑: 添加功能:

void add(){
  system("cls");
  contact* new = (contact*)malloc(sizeof(contact));
  char newname[MAX];
  char newnumber[MAX];
  printf("enter a name \n");
  gets(newname);
  printf("enter a number \n");
  gets(newnumber);
  new-> number = newnumber;
  new->name = newname;
  if(first== NULL){
    first= new;
  }
    else{
    if(first->name[0]>newname[0]){
      new->next = first;
      first = new;
    }
    else{
    contact* temp = first;
    while(temp->next!= NULL && temp->next->name[0]<newname[0]){  
      temp = temp-> next;
    }
    new->next = temp->next;
    temp->next = new;
    getch();

  }


}
file = fopen("file.txt", "w");
fprintf(file, "%s %s", newname, newnumber);
fclose(file);

system("cls");
menu();
}

【问题讨论】:

标签: c


【解决方案1】:

add 函数调用未定义行为(C 程序员的地狱),因为您在声明它们的函数返回后使用局部数组,并且只获得悬垂的指针:

void add(){
  system("cls");
  contact* new = (contact*)malloc(sizeof(contact)); // ok you allocated a contact
  char newname[MAX];            // Oops a local array...
  char newnumber[MAX];          // and another one
  ...
  new-> number = newnumber;     // new->number points to the local array
  new->name = newname;          // id. for new->name
  ...
}                               // newname and newnumber both reach end of life...

当 add 函数返回时,new-&gt;namenew-&gt;number 指向已经到达其生命终点并且正在悬垂的指针。取消引用它们显然是未定义的行为。

您应该使用 strdupnewnamenewnumber 的内容复制到动态内存中:

  ...
  new-> number = strdup(newnumber);   // new->number points to allocated memory: fine
  new->name = strdup(newname);        // id. for new->name
  ...

,或直接在 contact 结构中使用普通数组:

struct contact{
  char number[MAX];
  char name[MAX];
  struct contact* next;
};
...
void add(){
  system("cls");
  contact* new = (contact*)malloc(sizeof(contact));
  printf("enter a name 
");
  gets(new->name);
  printf("enter a number 
");
  gets(new->number);
  ...

不相关,但您应该停止使用已弃用的 gets,而只使用 fgetsgets 已被弃用,因为几十年来人们都知道它是造成大量(且无法避免)缓冲区溢出的原因。

【讨论】:

    【解决方案2】:

    如果您是来自阿尔及利亚的 hadjer brioua 并在土耳其学习,我找不到您的联系方式,请通过电子邮件 dr.yahia.boulahia@gmail.com 与我联系

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多