gpt4 book ai didi

C - 加载包含指向指针的指针的结构

转载 作者:太空狗 更新时间:2023-10-29 15:30:31 26 4
gpt4 key购买 nike

这会产生不兼容警告:

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

typedef struct
{
int key;
int data;
struct htData_* next;
struct htData_* prev;
}htData_;

typedef struct
{
int num_entries;
struct htData_** entries;
}ht_;

ht_* new_ht(int num_entries);
int ht_add(ht_* ht_p, int key, int data);

int main()
{
int num_entries = 20;
//crate a hash table and corresponding reference
ht_* ht_p = new_ht(num_entries);
//add data to the hash table
int key = 1305;
ht_add(ht_p,key%num_entries,20);

return 0;
}

ht_* new_ht(int num_entries)
{
ht_ *ht_p;
ht_ ht;
ht.num_entries = num_entries;
ht_p = &ht;

//create an array of htData
htData_ *htDataArray;
htDataArray = (htData_*) malloc(num_entries * sizeof(htData_));
//point to the pointer that points to the first element in the array
ht.entries = &htDataArray; // WARNING HERE!!!!!!!!!!!!!!!!

return ht_p;
}

我正在尝试将 **ptr 复制到包含 **ptrstruct

更新:我的简化代码不准确,所以我发布了实际代码。

最佳答案

问题是 struct htData_htData_ 不是同一件事!就编译器而言,struct htData_不存在——它是一个不完整的类型。 htData_ ,另一方面,是一个 typedef对于匿名结构。有关更详细的分析,请参阅 Difference between struct and typedef struct in C++ .

因此,您收到警告是因为 ht.entries被声明为类型 struct htData_** , 但该赋值的右侧具有类型 <anonymous struct>** .要解决此问题,您需要定义 struct htData_ :

typedef struct htData_
{
...
} htData_;

关于C - 加载包含指向指针的指针的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6825120/

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