【问题标题】:C: problem with operator '->', compile but return error. There are main.c and .h, .c filesC:操作符'->'有问题,编译但返回错误。有main.c和.h、.c文件
【发布时间】:2021-01-14 20:49:40
【问题描述】:

我是该网站的新手。我使用代码块。我正在使用 C 语言中的列表,我必须创建一个具有多个函数的程序,但它有一个头文件来声明它们和另一个 .c 文件来实现它们,然后调用 main.c 中的所有内容。当我编译代码时出现问题,插入 n 然后在进入 for 循环后立即按 enter 会出错。我相信'->'运算符有问题。你能帮帮我吗?

    //main.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <list.h>
    
    int main()
    {
        struct element *list;
        list = create_list();
    
        return 0;
    }
    //list.h
    
    #ifndef LIST_H_INCLUDED
    #define LIST_H_INCLUDED
    
    
    
    #endif // LIST_H_INCLUDED
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    struct element {
       int number;
       struct element *pointer;
    };
    
    struct element *create_list();
    //list.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include <list.h>
    
    int n = 0;
    
    struct element *create_list() {
    
       struct element *p, *ptr;
       int i;
    
       printf("Insert the number of element of the list... ");
       scanf("%d", &n);
    
       if (n==0) {
    
        p = NULL;
    
       } else {
    
        p = (struct element*)malloc(sizeof(struct element));
        p->number = 0;
        for(i=1; i<=n; i++) {
    
            //printf("ciao\n");
            ptr->pointer = (struct element *)malloc(sizeof(struct element));
            ptr = ptr->pointer;
            ptr->number = i;
        }
    
        ptr->pointer = NULL;
       }
    
    return(p);
    }

【问题讨论】:

  • 只是一般性评论:您应该将代码放在标头保护之间,而不是在它们之后。
  • @Cb95 你在这个语句中使用了未初始化的指针 ptr ptr->pointer = (struct element *)malloc(sizeof(struct element));
  • 包含保护应该保护整个文件,而不仅仅是前几行。你应该检查malloc的返回值。
  • @VladfromMoscow 这不是初始化吗?结构元素*p,*ptr;感谢回复
  • @Cb95 自动存储时长的变量声明,该变量应显式初始化。否则它们的值是不确定的。

标签: c list initialization structure singly-linked-list


【解决方案1】:

此声明

struct element *p, *ptr;

声明两个具有自动存储持续时间且具有不确定值的变量,因为它们没有显式初始化。

所以在这个for循环中

    for(i=1; i<=n; i++) {

        //printf("ciao\n");
        ptr->pointer = (struct element *)malloc(sizeof(struct element));
        ptr = ptr->pointer;
        ptr->number = i;
    }

您正在使用未初始化的指针ptr 试图访问不存在对象的数据成员pointer

似乎在循环之前您的意思是以下分配

    p = (struct element*)malloc(sizeof(struct element));
    p->number = 0;
    ptr = p;  // <===
    for(i=1; i<=n; i++) {

        //printf("ciao\n");
        ptr->pointer = (struct element *)malloc(sizeof(struct element));
        ptr = ptr->pointer;
        ptr->number = i;
    }

请注意,将全局变量 n 声明为有符号整数类型而不是无符号整数类型是一个坏主意。

如果用户输入一个负数,该函数将返回一个指向值为 0 的节点的指针的意外结果。

还将声明放在 #ifndef#endif 之间的标题中

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

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

struct element {
   int number;
   struct element *pointer;
};

struct element *create_list();    

#endif // LIST_H_INCLUDED

而且标头malloc.h 不是标准的C 标头。内存分配函数在标题&lt;stdlib.h&gt; 中。您的程序应该可以在没有标题 &lt;malloc.h&gt; 的情况下工作。所以删除它。

【讨论】:

    【解决方案2】:

    您使用以下行声明ptr

    struct element *p, *ptr;
    

    但是,在没有为其分配内存的情况下,您在此处取消引用指针 ptr:

    ptr->pointer = (struct element *)malloc(sizeof(struct element));
    

    这当然会导致分段错误。 您应该先为 ptr 分配内存,然后才能访问它的子元素。

    像这样,例如:

    ptr = (struct element*)malloc(sizeof(struct element));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多