这会产生不兼容警告:
#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
复制到包含struct
的{{1}}。
更新:我的简化代码不准确,所以我发布了实际代码。
答案 0 :(得分:4)
问题是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_;
答案 1 :(得分:1)
此行不合适:
htData_ array[20] = htDataArray;
您无法指定指向数组的指针。
在您编辑的代码中,以下是有问题的一行:
//point to the pointer that points to the first element in the array
ht.entries = &htDataArray;
实际上,从语法上讲它是正确的,所以它不应该发出警告。但你在这里做错了什么。如果你希望ht.entries
指向数组的第一个元素而不是你需要声明它,
htData_* entries; // 'struct' keyword not needed ahead of declaration
并将其指定为,
ht.entries = &htDataArray[0];