相关疑难解决方法(0)

Segfault来自添加变量

我当然是一个直接的C新手,但这让我很难过.我正在研究实践的链表实现,我只是通过向split_node函数添加一个变量来获得段错误:

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

struct Node {
    struct Node *child;
    char *content;
};

void print_list(struct Node node);
void split_node(struct Node *node, int position);

int main() {

    struct Node head, second, third;

    head.content = "first";
    second.content = "second";
    third.content = "i'm third";

    head.child = &second;
    second.child = &third;

    print_list(head);
    split_node(&head, 3);
    print_list(head);

    return 0;
}

void print_list(struct Node node) {
    printf("%s\n", node.content);
    if(node.child) print_list(*node.child);
}

    /*
    Split node into two nodes, with the first position characters of the …
Run Code Online (Sandbox Code Playgroud)

c segmentation-fault

2
推荐指数
1
解决办法
919
查看次数

标签 统计

c ×1

segmentation-fault ×1