【问题标题】:Pointer is not being assigned未分配指针
【发布时间】:2016-11-18 22:51:56
【问题描述】:

我正在为一个班级做这个小项目,我遇到了这个问题。 我在这个例子上运行了 ddd,当它退出 while 循环时 prev 设置为 null,导致分段错误。

当我运行 ddd 时,我注意到 temp 不为 null,并且两个“prev is nulln”打印语句不会被触发,但循环外的那个会触发。

你知道会发生什么吗?

        while(temp)
        {
            printf("while loop.\n");
            if(pe->y > temp->y)
            {
                printf("while - if reached.\n");
                break;
            }//end if statement

            prev = temp;
            if(prev == NULL)
                printf("prev is null1\n");
            temp = temp->next;
            if(prev == NULL)
                printf("prev is null2\n");
        }//end while loop
        if(prev == NULL)
            printf("prev is null3\n");
        prev->next = pe;//redit links so that node is added in.
        pe->next = temp;

【问题讨论】:

标签: c sorting graph linked-list


【解决方案1】:

如果这在循环的第一次迭代中成立:

    if(pe->y > temp->y)

那么你永远不会初始化prev

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2020-09-25
    • 2012-11-06
    • 1970-01-01
    • 2021-04-13
    • 2012-10-26
    相关资源
    最近更新 更多