繁体   English   中英

链表程序中的分段错误

[英]Segmentation fault error in Linked-List program

我在以下动态链表实现中遇到分段错误; GDB 显示问题出在链表( “LIST_CL.H”文件)的“ l_print ”函数中的node = node->next line。 任何人都可以帮忙吗? 我什至尝试在纸上进行“绘图调试”,但我仍然没有在这里遇到问题。

注意:我报告了插入和打印的完整代码,以防有用。 因此LIST.C文件包含用于插入元素的“switch case 6”(“ LIST_CL.H”文件中的“ l_add ”函数)和用于打印列表的“switch case 1”(“ LIST_CL.H ”中的 l_print”函数) 文件)

注意:此问题仅在列表包含一项或多项时发生。 列表为空时没有错误(“list_cl”结构的头节点和尾节点都为 NULL)

清单 C:

#include <stdio.h>
#include <string.h> //for strcpy
#include "cl_list.h"
#include "list_cl.h"

#define STRING_SIZE 25

int main(){
   list_cl class = L_EMPTYLIST_CL;
   
   puts("Select command:");
   puts("0. Exit.");
   puts("1. Insert node.");
   puts("6. Print all nodes in the linked list.");
   
   int k = 0;
   scanf("%d", &k);
   while(k != 0){
      switch(k){
         case 1:
            char cf[17] = "";
            char first_name[STRING_SIZE] = "";
            char last_name[STRING_SIZE] = "";
            getchar();
            
            puts("Insert name:");
            fgets(first_name, sizeof(first_name), stdin);
            puts("Insert surname:");
            fgets(last_name, sizeof(last_name), stdin);
            puts("Insert fiscal code:");
            fgets(cf, sizeof(cf), stdin);
            
            client cliente;
            strcpy(cliente.cf, cf);
            cliente.first_name = first_name;
            cliente.last_name = last_name;
            
            class = l_add_cl(class, cliente);
            
            puts("Node inserted.");
            
            break;
         case 6:
            l_print(class);
            break;
         default:
            break;
      }
      scanf("%d", &k);
   }
   
}

列表_CL.H:

list_cl l_add_cl(list_cl l, client p){

   l_node node;
   node.id = 1;
   node.person = p;
   node.next = NULL;

   if(l.head == NULL){
      //List is empty
      l.head = &node;
      l.tail = &node;
   } else {
      l.tail -> next = &node;
      l.tail = &node;
   }
   
   return l;
}

void l_print(list_cl l){
   l_node *node = NULL;
   node = l.head;
   while(node != NULL){
      //client *cliente = &node->person;
      //printf("ID Elemento: %d | Name: %s Surname: %s Fiscal Code: %s", node->id, cliente->first_name, cliente->last_name, cliente->cf);
      node = node->next; // SEGMENTATION FAULT ERROR HERE!
   }
}

CL_LIST.H:

#include "client.h"
typedef struct _node {
   unsigned int id;
   client person;
   struct _node *next;
} l_node;
typedef struct {
   l_node *head;
   l_node *tail;
} list_cl;
#define L_EMPTYLIST_CL {NULL,NULL}

客户端.H:

typedef struct {
   char cf[17];
   char *first_name;
   char *last_name;
} client;

固定代码:

清单 C:

#include <stdio.h>
#include <string.h> //for strcpy
#include "cl_list.h"
#include "list_cl.h"

#define STRING_SIZE 25

int main(){
   list_cl class = L_EMPTYLIST_CL;
   
   puts("Seleziona un comando:");
   puts("0. Exit.");
   puts("1. Insert one node.");
   puts("6. Print all nodes.");
   
   int k = 0;
   scanf("%d", &k);
   while(k != 0){
      switch(k){
         case 1:
            char cf[17] = "";
            //char first_name[STRING_SIZE] = ""; THIS CAUSE DATA INCONSISTENCY
            //char last_name[STRING_SIZE] = ""; THIS CAUSE DATA INCONSISTENCY
            char *first_name = malloc(sizeof(char)*STRING_SIZE);
            char *last_name = malloc(sizeof(char)*STRING_SIZE);

            getchar();
            
            puts("Insert name:");
            //fgets(first_name, sizeof(first_name), stdin); ISSUE WITH SIZE OF FIRST_NAME
            fgets(first_name, sizeof(char)*STRING_SIZE, stdin);
            puts("Insert surname:");
            //fgets(last_name, sizeof(last_name), stdin); ISSUE WITH SIZE OF LAST_NAME
            fgets(last_name, sizeof(char)*STRING_SIZE, stdin);
            puts("IInsert fiscal code:");
            fgets(cf, sizeof(cf), stdin);
            
            client *cliente = malloc(sizeof(client));
            strcpy(cliente->cf, cf);
            cliente->first_name = first_name;
            cliente->last_name = last_name;
            
            class = l_add_cl(class, *cliente);
            
            puts("Element inserted.");
            
            break;
         case 6:
            l_print(class);
            break;
         default:     
            break;
      }
      puts("Select command:");
      scanf("%d", &k);
   }
   
}

LIST_CL.H

list_cl l_add_cl(list_cl l, client p){

   l_node *node = malloc(sizeof(struct _node));
   node->id = 1;
   node->person = p;
   node->next = NULL;

   if(l.head == NULL){
      //Empty list
      l.head = node;
      l.tail = node;
   } else {
      l.tail -> next = node;
      l.tail = node;
      l.tail -> next = NULL;
   }
   
   return l;
}

void l_print(list_cl l){
   l_node *node = NULL;
   node = l.head;
   while(node != NULL){
      client *cliente = &node->person;
      printf("ID Element: %d | Name: %s Surname: %s Fiscal code: %s", node->id, cliente->first_name, cliente->last_name, cliente->cf);
      node = node->next;
   }
}

CL_LIST.H

#include "client.h"
typedef struct _node {
   unsigned int id;
   client person;
   struct _node *next;
} l_node;
typedef struct {
   l_node *head;
   l_node *tail;
} list_cl;
#define L_EMPTYLIST_CL {NULL,NULL}

客户端.H

typedef struct {
   char cf[17];
   char *first_name;
   char *last_name;
} client;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM