如何解决分段错误?

时间:2021-02-02 01:18:16

标签: c malloc

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

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;
}

the output i got

1 个答案:

答案 0 :(得分:2)

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

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