首页 > 解决方案 > 如何解决分段故障?

问题描述

调用此函数时出现分段错误错误

pterm *readterm()
{
    pterm *node;
    int exp;
    int coeff;
    printf("\nEnter exponent:");
    scanf("%d",&exp);
    printf("\nEnter exponent:");
    scanf("%d",&coeff);
    node=getnode();
    node->prev=NULL;
    node->exp=exp;
    node->coeff=coeff;
    node->next=NULL;
    return node;
}

我得到的输出

标签: cmalloc

解决方案


分段错误是由于访问“不属于你”的内存而导致的一种特定错误。这意味着如果你想在pterm *node这段代码中使用指针,你应该从内存中为指针占用空间。

据我所知,问题将出在函数上,getnode()因为如果它是正确的,那么内存就不会有任何问题。


推荐阅读