[英]Confusion about malloc an array of struct
好,所以我有以下结构
struct node {
int visited;
struct node **depend;
};
我正在尝试使用以下内容动态分配它
fscanf(iStream, "%d %d", &nTasks, &nRules);
graph = (struct node *) malloc(nTasks * sizeof(struct node));
但是Eclipse显示了一个
.. \\ GraphSort.c:62:18:警告:函数'malloc'的隐式声明[-Wimplicit-function-declaration] graph =(struct node *)malloc(nTasks * sizeof(struct node)); ^
和
.. \\ GraphSort.c:62:26:警告:内置函数'malloc'的不兼容隐式声明[默认启用] graph =(struct node *)malloc(nTasks * sizeof(struct node)); ^
我不明白是为什么。 数组不是表示为指向第一个元素的指针吗?
还有一点我有这个声明,它不显示警告
fscanf(iStream, "%d, %d", &taskId, &dependencies);
graph[taskId-1].visited = 0;
graph[taskId-1].depend = (struct node **) malloc(dependencies * sizeof(struct node *));
implicit declaration of function 'malloc'
表明您没有包含适当的头文件,该头文件告诉您的程序如何调用malloc
。 尝试添加到程序的开头:
#include <stdlib.h>
您的另一段代码不是“声明”,而是一系列语句。 编译器仅会警告您有关未能为其编译的每个文件声明malloc()
警告。
看来您只是忘记了包含<stdlib.h>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.