c - C 中的链表,大数的加法。分段故障

标签 c linked-list segmentation-fault

我正在做一个项目,我应该用 C 语言编写一个程序,我可以在其中添加最多 500 位数字的数字。

我认为我接近一个工作程序,但当我运行该程序时,我不断收到消息“Segmentation fault”。

我已经用谷歌搜索了这个,但似乎你可以从很多不同的原因中得到这个错误,你只需要弄清楚是哪个......

我对 C 不是很熟悉,所以我想你们也许可以帮助我?

到目前为止,这是我的代码:

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

// ----------------------------- STRUCTEN --------------------------

typedef struct node *nodeptr;

struct node{
    int data;
    nodeptr next;
};

// ----------------------------- MAIN -------------------------------

int main(){

    // Det krävs 3 listor. En för tal1, en för tal2 och en för svaret.

    nodeptr list1 = NULL;
    nodeptr list2 = NULL;
    nodeptr answer = NULL;

    // Allokerer minnet för listorna.

    list1 = malloc(sizeof(nodeptr));
    list2 = malloc(sizeof(nodeptr));
    answer = malloc(sizeof(nodeptr));

    creat_linked_list(list1, list2, answer);    // Skapar de länkade listorna.

    char first_number[1000];                    // Det första talet får max vara 1000 tecken.

    printf("Enter the first number: ");
    scanf("%s",first_number);

    char second_number[1000];

    printf("Enter the second number: ");
    scanf("%c",second_number);

    int l1 = fill_list(list1, first_number);
    int l2 = fill_list(list2, second_number);

    addition(list1, list2, answer);

return;
}

// ------------------------------ skapa den linkade listan -----------------------------

creat_linked_list (nodeptr list1, nodeptr list2, nodeptr answer){

    // Påbörjar listorna.

    list1 -> next = NULL;
    list2 -> next = NULL;
    answer -> next = NULL;

    list1 -> data = 0;
    list2 -> data = 0;
    answer -> data = 0;


}

// ------------------------------------ Fyller i listorna -----------------------------

int fill_list (nodeptr pointer, char number[]) {

    int x = 0;
    int lenght = strlen(number);

    while (x < lenght){

        int digit = (int)number[x] - (int)'0'; //'0' = 48, tas bort från number[x] för att det är ascii-kodat.
        nodeptr temp = NULL;
        temp = malloc(sizeof(nodeptr));
        temp -> next = pointer -> next;
        pointer -> next = temp;
        temp -> data = digit;
        x = x + 1;

    }
return lenght;
}

// --------------------------------------- Kod för addition av tal -------------------

addition(nodeptr list1, nodeptr list2, nodeptr answer){

    int digit1, digit2;
    int sum, carry;

    while(1){

        if ((list1 -> next != NULL)&&(list2 -> next != NULL)){

            list1 = list1 -> next;  //Tar ut plats i lista 1
            digit1 = list1 -> data; //Tar ut värdet på den platsen.

            list2 = list2 -> next;  // --- || --- 2
            digit2 = list2 -> data; // --- || ---
        }
        else{
            if((list1 -> next = NULL)&&(list2 ->next != NULL)) {

                digit1 = 0; // Eftersom att det inte finns fler siffror i tal 1

                list2 = list2 -> next;  // Samma som IF-satsen innan.
                digit2 = list2 -> data;
            }

            else{

                digit2 = 0; //// Eftersom att det inte finns fler siffror i tal 2

                list1 = list1 -> next;
                digit1 = list1 -> data;
            }
        }
        nodeptr temp = NULL;
        temp = malloc(sizeof(nodeptr));
        temp -> next = NULL;

        temp -> data = (digit1 + digit2 + carry)%10;
        answer -> next = temp;
        answer = answer -> next;

        if ((digit1 + digit2 + carry) > 9) {
            carry = 1;
        }
        else{
            carry = 0;
        }

        if((list1 -> next = NULL)&&(list2 ->next = NULL)) {
            break;
        }

    }

    if (carry){                     // Om det ligger kvar en carry efter att alla tal har blivit adderade
                                    // så går vi in här.
        nodeptr temp = NULL;
        temp = malloc(sizeof(nodeptr));

        answer -> next = temp;
        answer -> data = carry;     
        answer -> next = NULL;      // Markerar slutet av answer listan. 
    }
}

当我运行代码时,我到达了需要输入第一个数字的部分。之后代码崩溃,我得到了段错误。

请帮忙!

最佳答案

您的一个问题,以及最有可能导致崩溃的原因,很容易找到:

list1 = malloc(sizeof(nodeptr));
list2 = malloc(sizeof(nodeptr));
answer = malloc(sizeof(nodeptr));

那些 malloc 调用只分配 nodeptr 的大小,它是指向结构的指针,而不是结构本身。这将只有四个或八个字节,而您的结构可能有八个或十二个字节大。

关于c - C 中的链表,大数的加法。分段故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438399/

相关文章:

c - 如何使用 libxml2 解析 XML 中的数据?

c - 如何用链表实现O(M^2N)的两个多项式的乘法?

c - Valgrind 链表内存泄漏

添加元素时,java linkedlist 比 arraylist 慢?

c - mex 文件中的段错误

c - 段错误——无法理解错误的原因

C:传递二维数组的一维会导致段错误

c - 如何将 HFCLK 更改为由微处理器上的低频振荡器控制?

c++ - 乘法比浮点除法快吗?

c# - 在 C# 中调用 Cygwin GCC DLL 在 malloc 上挂起