gpt4 book ai didi

c - 我如何将 malloc 返回的结构类型转换为结构类型?

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:02 25 4
gpt4 key购买 nike

如何将 malloc 返回的地址强制转换为结构节点类型?
当我尝试编译以下代码时,它每次都显示有关类型更改的错误。

struct node {
int info;
struct node *link;
};
struct node createnode() {
struct node *n;
n = (struct node *) malloc( sizeof(struct node) );
// error: incompatible types when returning type 'struct node *' but 'struct node' was expected
return n;
}

最佳答案

您的 createnode 函数返回 struct node 但您返回的是 struct node*

您应该更改方法签名以使其返回一个struct node*

关于c - 我如何将 malloc 返回的结构类型转换为结构类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51505715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com