C 我的应用程序在没有任何错误的情况下崩溃,for循环出现问题(?)

C 我的应用程序在没有任何错误的情况下崩溃,for循环出现问题(?),c,C,我的老师给了我一个描述单一链表的库。我正在写一个主文件来测试它,我遇到了一个非常奇怪的问题 这是链表库的标题(有更多函数,但我只使用了一个): 这是我的.c文件: #include <stdlib.h> #include <stdio.h> #include "dssinglylinkedlist.h" struct _SListEntry { SListValue data; SListEntry *next; }; SListEntry *sli

我的老师给了我一个描述单一链表的库。我正在写一个主文件来测试它,我遇到了一个非常奇怪的问题

这是链表库的标题(有更多函数,但我只使用了一个):

这是我的.c文件:

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

#include "dssinglylinkedlist.h"

struct _SListEntry {
    SListValue data;
    SListEntry *next;
};

SListEntry *slist_append(SListEntry **list, SListValue data)
{
    /* Create new list entry */
    SListEntry *newEntry = malloc(sizeof(SListEntry));

    if (newEntry == NULL) {
        return NULL;
    }

    newEntry->data = data;
    newEntry->next = NULL;

    /* Hooking into the list is different if the list is empty */
    SListEntry *rover;
    if (*list == NULL) {
        /* Create the start of the list */
        *list = newEntry;
    } else {
        /* Find the end of list */
        rover=*list;
        while (rover->next != NULL) {
            rover = rover->next;
        }

        /* Add to the end of list */
        rover->next = newEntry;
    }
    return newEntry;
}
SListValue slist_nthData(SListEntry *list, unsigned int n)
{
    /* Find the specified entry */
    SListEntry *entry = slist_nthEntry(list, n);

    /* If out of range, return NULL, otherwise return the data */
    if (entry == NULL) {
        return SLIST_NULL;
    } else {
        return entry->data;
    }
}
unsigned int slist_length(SListEntry *list)
{
    SListEntry *entry = list;
    unsigned int length = 0;

    while (entry != NULL) {
        /* Count the number of entries */
        ++length;
        entry = entry->next;
    }
    return length;
}
#包括
#包括
#包括“dssinglylinkedlist.h”
结构工程{
滑动值数据;
滑动*下一步;
};
SListEntry*slist\u append(SListEntry**列表,SListValue数据)
{
/*创建新的列表条目*/
SListEntry*newEntry=malloc(sizeof(SListEntry));
if(newEntry==NULL){
返回NULL;
}
新建条目->数据=数据;
newEntry->next=NULL;
/*如果列表为空,则挂接到列表是不同的*/
斯莱斯特里*罗孚;
如果(*list==NULL){
/*创建列表的开头*/
*列表=新条目;
}否则{
/*找到列表的末尾*/
罗孚=*列表;
while(漫游者->下一步!=NULL){
漫游者=漫游者->下一步;
}
/*添加到列表的末尾*/
漫游者->下一步=新建条目;
}
返回新条目;
}
SListValue slist\n数据(SListEntry*列表,无符号整数n)
{
/*查找指定的条目*/
SListEntry*entry=slist\n条目(列表,n);
/*如果超出范围,则返回NULL,否则返回数据*/
if(条目==NULL){
返回SLIST_NULL;
}否则{
返回条目->数据;
}
}
无符号整数滑动长度(滑动*列表)
{
SListEntry*条目=列表;
无符号整数长度=0;
while(条目!=NULL){
/*计算条目的数量*/
++长度;
进入=进入->下一步;
}
返回长度;
}
我认为这些代码没有问题。 我正在使用这个库编写一个main来存储两个多项式的信息:

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

#include "dssinglylinkedlist.h"

void printPolynomial(SListEntry*);

int main()
{
    SListEntry *poly1;
    int n;

    printf("Input n for poly1: ");
    scanf("%d", &n);

    printf("Input poly1: ");
    for (int i=0; i<=n; i++) {
        int  *temp = (int *) malloc(sizeof (int));
        scanf("%d", temp);
        slist_append(&poly1, temp);
    }

    printPolynomial(poly1);

    SListEntry *poly2;
    printf("Input n for poly2: ");
    scanf("%d", &n);
    printf("Input poly2: ");
//    for (int i=0; i<=n; i++) {
//        int  *temp = (int *) malloc(sizeof (int));
//        scanf("%d", temp);
//        slist_append(&poly2, temp);
//    }

//    printPolynomial(poly2);

    return 0;
}

void printPolynomial(SListEntry *list)
{
    int length = (int)slist_length(list);
    for (int i = 0; i < length; i++) {
        int temp = *((int *)slist_nthData(list,(unsigned int)i));
        if (i >0 && temp >= 0) {
            printf("+");
        }
        printf("%d*x^%d", temp, length-i-1);
    }
    printf("\n");
}
#包括
#包括
#包括“dssinglylinkedlist.h”
虚空打印多项式(SListEntry*);
int main()
{
SListEntry*poly1;
int n;
printf(“poly1的输入n:”);
scanf(“%d”和“&n”);
printf(“输入poly1:”);
对于(int i=0;i=0){
printf(“+”);
}
printf(“%d*x^%d”,温度,长度-i-1);
}
printf(“\n”);
}
因此,如果我对第二个循环进行注释(在我打印“Input poly2”之后,就像我上面写的那样),我仍然可以输入我的poly1并打印它。但是如果我取消注释它,当我附加poly1的第一个元素时,我的程序会立即崩溃。
我的代码有什么问题?

考虑使用
-g
(debug)编译,并在调试器下运行。您可能会发现问题。poly1和poly2是未初始化的变量。@dash-o我尝试了qt的调试模式,它指向一行有分段错误的代码。但这条线只适用于一个多边形。只有当我尝试输入poly2时,它才会崩溃。您是否尝试用NULL初始化poly1和poly2?@user5329483谢谢,我刚刚尝试初始化NULL。而且它工作得很好!我想我可以像我写的那样写struct,而不需要初始化任何东西。我一定错了。
#include <stdio.h>
#include <stdlib.h>

#include "dssinglylinkedlist.h"

void printPolynomial(SListEntry*);

int main()
{
    SListEntry *poly1;
    int n;

    printf("Input n for poly1: ");
    scanf("%d", &n);

    printf("Input poly1: ");
    for (int i=0; i<=n; i++) {
        int  *temp = (int *) malloc(sizeof (int));
        scanf("%d", temp);
        slist_append(&poly1, temp);
    }

    printPolynomial(poly1);

    SListEntry *poly2;
    printf("Input n for poly2: ");
    scanf("%d", &n);
    printf("Input poly2: ");
//    for (int i=0; i<=n; i++) {
//        int  *temp = (int *) malloc(sizeof (int));
//        scanf("%d", temp);
//        slist_append(&poly2, temp);
//    }

//    printPolynomial(poly2);

    return 0;
}

void printPolynomial(SListEntry *list)
{
    int length = (int)slist_length(list);
    for (int i = 0; i < length; i++) {
        int temp = *((int *)slist_nthData(list,(unsigned int)i));
        if (i >0 && temp >= 0) {
            printf("+");
        }
        printf("%d*x^%d", temp, length-i-1);
    }
    printf("\n");
}