C语言 内置函数“malloc”的不兼容隐式声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:23:14  来源:igfitidea点击:

Incompatible implicit declaration of built-in function ‘malloc’

cstructmalloc

提问by SGE

I'm getting this error:

我收到此错误:

warning: incompatible implicit declaration of built-in function ‘malloc'

警告:内置函数“malloc”的隐式声明不兼容

I am trying to do this:

我正在尝试这样做:

fileinfo_list* tempList = malloc(sizeof(fileinfo_list));

Just for the reference the struct used at hand is:

仅供参考,手头使用的结构是:

typedef struct {
    fileinfo** filedata;
    size_t nFiles;
    size_t size;
    size_t fileblock;
} fileinfo_list;

I don't see anything wrong with what I've done. I'm just creating a tempListwith the size of 1 x fileinfo_list.

我认为我所做的没有任何问题。我只是创建一个tempList大小为 1 x fileinfo_list

回答by cnicutar

You likely forgot to include <stdlib.h>.

您可能忘记包含<stdlib.h>.

回答by Omri Barel

You need to #include <stdlib.h>. Otherwise it's defined as int malloc()which is incompatible with the built-in type void *malloc(size_t).

你需要#include <stdlib.h>。否则它被定义为int malloc()与内置类型不兼容void *malloc(size_t)

回答by Antti

You're missing #include <stdlib.h>.

你不见了#include <stdlib.h>

回答by santosh sahu

The stdlib.h file contains the header information or prototype of the malloc, calloc, realloc and free functions.

stdlib.h 文件包含 malloc、calloc、realloc 和 free 函数的头信息或原型。

So to avoid this warning in ANSI C, you should include the stdlib header file.

因此,为了避免 ANSI C 中出现此警告,您应该包含 stdlib 头文件。

回答by user3828152

The only solution for such warnings is to include stdlib.h in the program.

此类警告的唯一解决方案是在程序中包含 stdlib.h。