c - 警告 : return makes pointer from integer without a cast using malloc sizeof struct in C

标签 c pointers struct malloc

<分区>

因此,当我尝试创建指向结构的指针时,我收到了一个警告:返回从没有转换的整数生成指针

结构:

typedef struct _book_
{
    char *title;
    char *cat;
    double price;
}Book_T;

代码:

Book_T *book;
...
if((book = malloc(sizeof(Book_T))) == NULL) return 1;

该结构在不同的 .h 文件中声明,但我仔细检查了是否包含它。我有点迷失在这个上

最佳答案

在 shf301 和 Brian 的帮助下回答我自己的问题,问题是实际的返回类型,与 malloc() 无关。正确的代码应该是这样的:

if((book = malloc(sizeof(Book_T))) == NULL) return NULL;

关于c - 警告 : return makes pointer from integer without a cast using malloc sizeof struct in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13889794/

相关文章:

c - 收到一条错误消息,指出该变量在已声明时尚未声明

c - 使用其他 Const Struct 实例初始化 Const Struct

c - Printf() 打印字符为 32 位

c++ - 从指针转换为对象 C++

c - 结构体中的指针问题

我可以使用 & 运算符在函数中获取有效指针,即使该函数已在 golang 中返回

c# - 为什么结构对齐取决于字段类型是原始类型还是用户定义的?

c - 在 GTK 中调整绘图区域的大小

在 Mac OSX 上安装 C 库

c - 为什么我的 (Leetcode #231) Power of Two 给出了错误的结果?