C: 运算符 '->' 问题,编译但 return 错误。有main.c和.h,.c文件
C: problem with operator '->', compile but return error. There are main.c and .h, .c files
我是这个网站的新手。我使用代码块。我正在使用 C 中的列表,我必须创建一个具有多个函数的程序,但它有一个 header 来声明它们,另一个 .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);
}
您使用以下行声明 ptr
:
struct element *p, *ptr;
但是,在没有为其分配内存的情况下,您在此处取消引用指针 ptr:
ptr->pointer = (struct element *)malloc(sizeof(struct element));
这当然会导致分段错误。
您应该先为 ptr 分配内存,然后才能访问其 sub-elements.
像这样,例如:
ptr = (struct element*)malloc(sizeof(struct element));
这个声明
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
试图访问 non-existent object.
的数据成员 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 是一个坏主意,而且它是有符号整数类型而不是无符号整数类型。
如果用户将输入一个负数,该函数将 return 指向值为 0 的节点的指针的意外结果。
同时在 #ifndef
和 #endif
之间的 header 中放置声明
#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
而headermalloc.h
不是标准的Cheader。内存分配函数在 header <stdlib.h>
中。您的程序应该可以在没有 header <malloc.h>
的情况下运行。所以删除它。
我是这个网站的新手。我使用代码块。我正在使用 C 中的列表,我必须创建一个具有多个函数的程序,但它有一个 header 来声明它们,另一个 .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);
}
您使用以下行声明 ptr
:
struct element *p, *ptr;
但是,在没有为其分配内存的情况下,您在此处取消引用指针 ptr:
ptr->pointer = (struct element *)malloc(sizeof(struct element));
这当然会导致分段错误。 您应该先为 ptr 分配内存,然后才能访问其 sub-elements.
像这样,例如:
ptr = (struct element*)malloc(sizeof(struct element));
这个声明
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
试图访问 non-existent object.
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 是一个坏主意,而且它是有符号整数类型而不是无符号整数类型。
如果用户将输入一个负数,该函数将 return 指向值为 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
而headermalloc.h
不是标准的Cheader。内存分配函数在 header <stdlib.h>
中。您的程序应该可以在没有 header <malloc.h>
的情况下运行。所以删除它。