这是一段代码的摘录,我在其中用数组的元素填充了一个列表。
#include <stdlib.h>
#include <stdio.h>
#include "../../lib/kernel/list.h"
#include "./listpop.h"
struct item {
struct list_elem elem;
int value;
int priority;
};
void populate(struct list * l, int * a, int n);
void populate(struct list * l, int * a, int n)
{
int i = 0;
while(i != n) {
struct item * newitem = malloc(sizeof(struct item));
newitem->value = a[i];
list_push_back(l,newitem);
i++;
}
}
void test_assignment_1()
{ struct list our_list;
list_init(&our_list);
populate(&our_list, ITEMARRAY, ITEMCOUNT);
}
list.h 中的代码:
/* List element. */
struct list_elem
{
struct list_elem *prev; /* Previous list element. */
struct list_elem *next; /* Next list element. */
};
/* List. */
struct list
{
struct list_elem head; /* List head. */
struct list_elem tail; /* List tail. */
};
void list_init (struct list *);
list.c 中的代码:
/* Initializes LIST as an empty list. */
void
list_init (struct list *list)
{
ASSERT (list != NULL);
list->head.prev = NULL;
list->head.next = &list->tail;
list->tail.prev = &list->head;
list->tail.next = NULL;
}
最后,listpop.h 中的代码:
#define ITEMCOUNT 10
int ITEMARRAY[ITEMCOUNT] = {3,1,4,2,7,6,9,5,8,3};
这是我收到的警告:
警告:函数“malloc”的隐式声明
警告:内置函数“malloc”的隐式声明不兼容
到目前为止,我所读到的有关这些警告的所有信息都是添加 stdlib.h,但正如您从我的代码中看到的那样,我已经完成了,并且代码仍然给我这些警告。我已经多次重新启动代码,所以错误位于代码中的某处。
有人知道什么在这里不起作用吗?
最佳答案
您可能正在使用不符合标准的编译器和/或 C 库在过时的系统上进行编译。尝试包括 <malloc.h>
除了<stdlib.h>
并始终首先包含标准 header 。
关于c - 即使包含 <stdlib.h> 也会出现警告 : implicit declaration of function ‘malloc’ ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61826531/