c - 结构数组 : assignment from incompatible pointer type

标签 c pointers struct

对于太具体的问题,我很抱歉,但指针特技对我来说非常困难,以我的 Py 背景。

已编辑

我想声明一个结构数组:

struct key {         
  int* data;         
  struct node* left; 
  struct node* right;
};                   

//There is a problem too. Is it possible to return an array of structs from a function.                 
struct key *NewNode(int value) { 
  struct key** new_node;                          
  struct key* new_key;                            

  new_node = (struct key*)malloc(sizeof(new_key)); // warning is here
  new_key  = malloc(sizeof *new_key);             

  new_key->data = value;                          
  new_key->left = NULL;                           
  new_key->right = NULL;                          

  new_node[0] = new_key;                          

  return new_node;                      
}                 

// Somewhere in further code: 
realloc(new_node, sizeof *key);
new_node[1] = new_key; // Next key passed to the node.                               

如何在没有此警告的情况下声明结构数组?

assignment from incompatible pointer type

请向我描述一下,为什么会这样?因为我仍然相信数组成员的类型是struct key

最佳答案

这里有几个问题。

首先,我假设这段代码的目的是分配和初始化struct node 类型的单个对象;因此,new_node 是多余的;它没有任何用处。干脆彻底去掉,直接返回new_key即可。

其次,

new_key  = malloc(sizeof(new_key));

不会分配足够的内存来存储struct node类型的对象;它只分配足够的空间来存储一个指针struct node,因为expressionnew_key的类型是结构节点*。您需要将该行更改为

new_key = malloc( sizeof *new_key );

注意sizeof是一个运算符,不是一个函数;仅当操作数是类型名称如 intfloatstruct whatchamacallit 时才需要括号。

最后一行

new_key->data = value;

是一个危险信号;根据您调用 NewNode 的方式,该指针可能最终对于您的所有节点都是相同的。如果您的来电者看起来像这样:

while ( not_done_yet )
{
  x = get_input();
  node = NewNode( &x );
  root = insert( root, node );
}

所有 树中的节点将最终指向同一事物 (x)。更糟糕的是,如果 x 超出范围,那么所有这些指针都会突然变得无效

如果您的节点打算存储单个整数值,那么最好将参数和 data 成员都设为常规 int,而不是指向 整数

总结:

struct node {         
  int data;         
  struct node* left; 
  struct node* right;
};                   

struct node *NewNode(int value) 
{
  struct node* new_key = malloc(sizeof *new_key);
  if ( new_key )
  {
    new_key->data = value;
    new_key->left = NULL;
    new_key->right = NULL;
  }
  return new_key;  
}                   

编辑

如果要创建节点数组,请在调用 NewNode 的函数中执行,而不是在 NewNode 函数本身中执行:

#define INITIAL_SIZE 1

struct node *CreateNodeArray( size_t *finalArraySize )
{
  *finalArraySize = 0;
  size_t i = 0;

  /**
   * For the purpose of this example, we'll start with an array of size
   * 1 and double it each time we hit the limit, but in practice you'd
   * want to start with a minimum size large enough to handle most
   * cases.
   */
  struct node *new_node = malloc( sizeof *new_node * INITIAL_SIZE );
  if ( new_node )
  {
    *finalArraySize = INITIAL_SIZE;

    while ( there_is_more_data() )
    {
      /**
       * First, check to see if we've hit the end of our array
       */
      if ( i == *finalArraySize )
      {
        /**
         * We've hit the end of the array, so we need to extend
         * it; in this case, we'll double its size
         */
        struct node *tmp = realloc( new_node, 
                                    sizeof *new_node * (*finalArraySize * 2) );              
        if ( tmp )
        {
          *finalArraySize *= 2;
          new_node = tmp;
        }
      }

      /**
       * Add a new node to the array
       */
      int data = getInput();
      new_node[i++] = NewNode( data ); 
    }
  }
  return new_node;
}

这段代码对您如何构建单个节点做出了一些假设,但它应该可以说明我要表达的观点,即您希望将构建节点数组与构建单个节点完全分开。

关于c - 结构数组 : assignment from incompatible pointer type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22796614/

相关文章:

c++ - 递归N-骑士问题

c - 反转数组中的字符单词

C++:C++11 能否移动语义以避免共享所有权情况下的指针?

struct - D(2) 编程 : chaining functions call on struct

c - 检索库运行时

关于 argc 和 argv 的说明

C 在 Windows : Bind to a free random port and get the port number

pointers - 从 reflect.Value 中提取 uintptr

c - 将结构指针发送到 C 中的函数

c - 从二进制文件读取数据到 C 结构中的访问冲突