简体   繁体   English

C - 将包含指针的结构加载到指针

[英]C - Loading a Struct Containing a Pointer to a Pointer

This produces an incompatibility warning: 这会产生不兼容警告:

#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;
}

I'm trying to copy the **ptr to the struct containing a **ptr . 我正在尝试将**ptr复制到包含**ptrstruct中。

Update: My simplified code was not accurate so I've posted the actual code. 更新:我的简化代码不准确,所以我发布了实际的代码。

The problem is that struct htData_ and htData_ are not the same thing! 问题是struct htData_htData_ 不是一回事! As far as the compiler is concerned, struct htData_ doesn't exist—it's an incomplete type. 就编译器而言, struct htData_不存在 - 它是一个不完整的类型。 htData_ , on the other hand, is a typedef for an anonymous structure. 另一方面, htData_是匿名结构的typedef For a more detailed analysis, see Difference between struct and typedef struct in C++ . 有关更详细的分析,请参阅C ++中structtypedef struct之间的区别

So, you're getting a warning because ht.entries is declared as the type struct htData_** , but the right-hand side of that assignment has type <anonymous struct>** . 所以,你得到一个警告,因为ht.entries被声明为struct htData_**类型,但是该赋值的右侧有<anonymous struct>**类型。 To fix this, you need to define struct htData_ : 要解决此问题,您需要定义struct htData_

typedef struct htData_
{
    ...
} htData_;

This line is not proper: 这条线不合适:

htData_ array[20] = htDataArray;

You cannot assign a pointer to an array. 您不能指定指向数组的指针。

In your edited code, here is the problematic line: 在您编辑的代码中,这是有问题的行:

//point to the pointer that points to the first element in the array                                                                                          
  ht.entries = &htDataArray;

Actually, syntactically it's correct, so it should not give warning. 实际上,在语法上它是正确的,所以它不应该发出警告。 But you are doing wrong stuff here. 但你在这里做错了什么。 If you want ht.entries pointing to the first element of array than you need to declare it as, 如果你想要ht.entries指向数组的第一个元素而不是你需要声明它,

htData_* entries;  // 'struct' keyword not needed ahead of declaration

and assign it as, 并指定为,

ht.entries = &htDataArray[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM