You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言链表头部插入元素代码运行崩溃、无限递归编译错误如何解决?

问题原因
  • 自定义print()函数内部存在笔误:原本需要调用C标准库的输出函数printf("List is: "),错误写为调用print("List is: "),会导致函数进入无限递归调用,没有任何终止条件,最终触发栈溢出导致程序崩溃。
  • VS Code默认编译参数没有开启无限递归相关的报错规则,因此可以编译通过,但运行时直接崩溃;CS50 IDE的编译参数添加了-Werror,-Winfinite-recursion规则,会在编译阶段就识别出这种无限递归问题并抛出错误。
解决方法

修改print()函数内的错误调用即可,将print("List is: ");替换为printf("List is: ");,修改后的完整print函数代码如下:

void print()
{
    node* temp = head;

    printf("List is: ");

    while(temp != NULL)
    {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 05:24:05