程序无法在eclipse上运行

时间:2018-01-14 12:16:26

标签: c eclipse

我做了一个简单的程序,在链表的头部插入三个值。 它似乎在这个在线编译器https://www.onlinegdb.com/online_c_compiler上正常工作, 然而,当我尝试在eclipse(或devcpp)中运行它时,它会崩溃。

我正在使用Windows 10和MinGW GCC编译器

以下是代码:

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

struct list {
    float value;
    struct list * next_ptr;
};

void init (struct list **ptrptr);

void pre_insert(struct list **ptrptr, float value);

void visit(struct list * ptr);



int main()
{
    struct list ** ptrptr;
    init (ptrptr);

    float value_1 = 47.2;
    float value_2 = 13.23;
    float value_3 = 3.4;


    pre_insert(ptrptr, value_1);
    pre_insert(ptrptr, value_2);
    pre_insert(ptrptr, value_3);


    visit(*ptrptr);
    return 0;
}


void init (struct list** ptrptr){
    *ptrptr=NULL;
}

void pre_insert(struct list ** ptrptr, float value){
    struct list * tmp_ptr = *ptrptr;
    *ptrptr = (struct list *)malloc(sizeof(struct list));
    (*ptrptr)->value=value;
    (*ptrptr)->next_ptr=tmp_ptr;
}

void visit(struct list * ptr) {
    while(ptr!= NULL){
        printf ("(%f)\n",ptr->value);
        ptr = ptr->next_ptr;
    }
    printf("\n");
}

2 个答案:

答案 0 :(得分:0)

你好Giuliano Bellucci,

您的segmentation fault功能正在获得init()

我对您的代码进行了一些更改,现在运行时没有任何错误 -

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

struct list {
    float value;
    struct list * next_ptr;
};

struct list* init (struct list *ptrptr);

void pre_insert(struct list *ptrptr, float value);

void visit(struct list * ptr);



int main()
{
    struct list  *head;


    head = init (head);
    float value_1 = 47.2;
    float value_2 = 13.23;
    float value_3 = 3.4;



    pre_insert(head, value_1);    
    pre_insert(head, value_2);
    pre_insert(head, value_3);


    visit(head);
    return 0;
}


struct list * init (struct list *head)
{
    head = (struct list *) malloc( sizeof(struct list) );
    head -> next_ptr = NULL;
    return head;
}

void pre_insert(struct list *head, float value){

    struct list *new_node = (struct list *)malloc(sizeof(struct list));
    new_node->value=value;
    new_node->next_ptr = head->next_ptr;
    head->next_ptr=new_node;
}

void visit(struct list *head) {
    struct list *mobile = (struct list *) malloc(sizeof(struct  list));
    mobile = head -> next_ptr;

    while(mobile!= NULL){
        printf ("(%f)\n",mobile->value);
        mobile = mobile->next_ptr;
    }
    printf("\n");
}

答案 1 :(得分:0)

您的结构list ** ptrptr未初始化并指向随机内存。 这将导致随机崩溃(segmentation fault)。 初始化指针:

struct list ** ptrptr = malloc (sizeof(struct list));

GCC 7.2.0编制的工作计划:

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

struct list {
    float value;
    struct list * next_ptr;
};

void init (struct list **ptrptr);
void pre_insert(struct list **ptrptr, float value);
void visit(struct list * ptr);


void init (struct list** ptrptr){
    *ptrptr = NULL;
}

void pre_insert(struct list ** ptrptr, float value){

    struct list * tmp_ptr = *ptrptr;

    *ptrptr = /*(struct list *)*/ malloc (sizeof(struct list));

    (*ptrptr)->value = value;
    (*ptrptr)->next_ptr = tmp_ptr;
}

void visit(struct list * ptr) {

    while(ptr!= NULL){

        printf ("(%f)\n",ptr->value);
        ptr = ptr->next_ptr;
    }
    printf("\n");
}

int main()
{
    struct list ** ptrptr = malloc (sizeof(struct list));

    init (ptrptr);

    float value_1 = 47.2;
    float value_2 = 13.23;
    float value_3 = 3.4;

    pre_insert(ptrptr, value_1);
    pre_insert(ptrptr, value_2);
    pre_insert(ptrptr, value_3);


    visit(*ptrptr);

    return 0;
}

输出:

(3.400000)
(13.230000)
(47.200001)