如果我在我的代码中使用malloc
:
int *x = malloc(sizeof(int));
new.c:7: warning: implicit declaration of function ‘malloc’
new.c:7: warning: incompatible implicit declaration of built-in function ‘malloc’
需要添加:
#include <stdlib.h>
该文件包括内置函数malloc
的声明。 如果你不这样做,编译器会认为你想定义你自己的名为malloc
的函数,它会警告你,因为:
- 你没有明确声明它和
- 已经有一个内置函数,该名称具有与隐式声明的不同的签名(当一个函数被隐式声明时,它的返回和参数类型被假定为
int
,这与内置的不兼容-inmalloc
,它需要一个size_t
并返回一个void*
)。