You need to enable JavaScript to run this app. C语言单链表存储Person结构体出现段错误的代码修正求助
优惠活动
大模型
产品
解决方案
定价
更多

C语言单链表存储Person结构体出现段错误的代码修正求助

C语言单链表存储人员信息段错误问题修正

问题背景

实现将人员信息结构体存入单链表并执行新增、遍历操作时,运行到输入结束后触发段错误,以下是初始实现代码:

1 人员信息结构体定义

struct Person {
    char name[MAX];
    char id[MAX];
};

2 单链表节点结构体定义

struct Node {
    struct Person* pPerson;
    struct Node* pNext;
};

3 新建Person实例函数

struct Person* newPerson() {
    Person* pPerson = NULL;
    pPerson = (Person*)malloc(sizeof(Person));
    printf("name: "); scanf("%s", pPerson->name);
    printf("id: "); scanf("%s", pPerson->id);
    
    return pPerson;
}

4 新建链表节点函数

struct Node* newNode(Person* pNewPerson) {
    Node* pPeople = NULL;
    pPeople = (struct Node*)malloc(sizeof(struct Node));
    pPeople->pPerson = pNewPerson;
    return pPeople;
}

5 链表尾部插入函数

void insert(Node* pHead, Person* pNewPerson) {
    Node* pCurrent = pHead;
    while (pCurrent != NULL) {
        pCurrent = pCurrent->pNext;
    }
    
    pCurrent = (Node*)malloc(sizeof(Node));
    pCurrent->pNext->pPerson = pNewPerson;
    pCurrent->pNext = NULL;
}

6 链表遍历函数

void display(Node* pHead) {
    Node* pCurrent = pHead;
    int index = 1;
    
    while (pCurrent != NULL) {
        printf("Person %d\n", index);
        printf("name: %s\n", pCurrent->pPerson->name);
        printf("id: %s\n", pCurrent->pPerson->id);
        index += 1;
        pCurrent = pCurrent->pNext;
    }
}

7 测试代码

Node* pNode = NULL;
Person* pPerson = NULL;
pPerson = newPerson();
pNode = newNode(pPerson);
insert(pNode, pPerson);
display(pNode);

核心问题说明

  1. newNode未初始化pNext指针:新创建的节点pNext为野指针,后续访问时直接触发非法内存访问。
  2. insert函数逻辑完全错误:
    • 循环条件使用while(pCurrent != NULL),遍历结束后pCurrent已经指向NULL,后续申请的新节点无法挂载到原链表尾部,属于完全游离的无效节点。
    • 对刚分配的pCurrent直接访问pCurrent->pNext->pPerson,此时pNext还是野指针,直接访问其成员是触发段错误的直接原因。
  3. 测试逻辑存在冗余:已经通过newNode创建了头节点存储第一个Person实例,再次调用insert插入同一个Person属于无意义的重复操作。
  4. 原代码缺失必要的头文件引入、宏定义和类型别名,编译阶段就会存在报错点。

修正后的完整可用代码

#include <stdio.h>
#include <stdlib.h>
#define MAX 20 // 可根据实际需求调整字符串长度上限

typedef struct Person {
    char name[MAX];
    char id[MAX];
} Person;

typedef struct Node {
    struct Person* pPerson;
    struct Node* pNext;
} Node;

struct Person* newPerson() {
    Person* pPerson = (Person*)malloc(sizeof(Person));
    // 实际生产环境可添加malloc返回值判空逻辑,避免内存申请失败触发问题
    printf("name: "); scanf("%s", pPerson->name);
    printf("id: "); scanf("%s", pPerson->id);
    return pPerson;
}

struct Node* newNode(Person* pNewPerson) {
    Node* pPeople = (struct Node*)malloc(sizeof(struct Node));
    pPeople->pPerson = pNewPerson;
    pPeople->pNext = NULL; // 初始化pNext为NULL,消除野指针
    return pPeople;
}

void insert(Node* pHead, Person* pNewPerson) {
    if (pHead == NULL) return; // 头节点为空直接返回,避免非法访问
    Node* pCurrent = pHead;
    // 遍历到最后一个节点就停止,而不是遍历到NULL
    while (pCurrent->pNext != NULL) {
        pCurrent = pCurrent->pNext;
    }
    // 创建新节点挂载到链表尾部
    pCurrent->pNext = newNode(pNewPerson);
}

void display(Node* pHead) {
    Node* pCurrent = pHead;
    int index = 1;
    while (pCurrent != NULL) {
        printf("Person %d\n", index);
        printf("name: %s\n", pCurrent->pPerson->name);
        printf("id: %s\n", pCurrent->pPerson->id);
        index += 1;
        pCurrent = pCurrent->pNext;
    }
}

// 补充链表内存释放函数,避免内存泄漏
void freeList(Node* pHead) {
    Node* pTemp = NULL;
    while(pHead != NULL) {
        pTemp = pHead;
        pHead = pHead->pNext;
        free(pTemp->pPerson);
        free(pTemp);
    }
}

int main() {
    // 创建第一个节点作为链表头
    Person* pPerson1 = newPerson();
    Node* pHead = newNode(pPerson1);
    // 测试插入第二个节点
    Person* pPerson2 = newPerson();
    insert(pHead, pPerson2);
    // 遍历输出
    display(pHead);
    // 释放内存
    freeList(pHead);
    return 0;
}

内容的提问来源于stack exchange,提问作者frankiie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 02:15:07