gpt4 book ai didi

c - Visual Studio 2019使用typedef数据类型获取错误C2440

转载 作者:行者123 更新时间:2023-12-02 10:56:07 25 4
gpt4 key购买 nike

我遇到了这个问题,不知道如何解决:
该错误:

error C2440: '=': cannot convert from 'void *' to 'node_t'
代码是:
node_t* arr = malloc(sizeof(node_t) * temp3);
for (int i = 0; i < temp3; i++)
arr[i] = NULL;
谢谢。

最佳答案

arr[i]的类型是node_t,它不是指针类型(我从错误消息中猜到了)。
此代码重现了问题:

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int a, b;
} node_t;


int main() {
node_t* arr = malloc(sizeof(node_t) * 10);
for (int i = 0; i < 10; i++)
arr[i] = NULL;
}
您可能需要这样的东西:
void initialize_node(node_t *node)
{
// adapt this to your actual node_t type
node->a = 0;
node->b = 0;
}

int main() {
node_t* arr = malloc(sizeof(node_t) * 10);
for (int i = 0; i < 10; i++)
initialize_node(&arr[i]);
}

关于c - Visual Studio 2019使用typedef数据类型获取错误C2440,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64011505/

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