简体   繁体   English

“一元'*'的类型参数无效”错误

[英]“Invalid type argument of unary '*' ” error

I am trying to build a tree with nodes but I have a pointer problem我正在尝试用节点构建一棵树,但我有一个指针问题

I want to build a node but when I try, my program throw me an error.我想建立一个节点,但是当我尝试时,我的程序给我一个错误。

I tried removing the "*" but it gave me an other error and made no sense (return a struct instead of the address of a struct?:) but I don't know where's the error:我尝试删除“*”,但它给了我另一个错误并且没有任何意义(返回一个结构而不是结构的地址?:) 但我不知道错误在哪里:

Here's my code:这是我的代码:

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

typedef struct node node;

struct node{
    node *leftson;
    node *rightson;
    int val;
};

node * node_create( int value, node *left, node *right){
    malloc(sizeof(node));
    node n = {left, right, value};
    return (*n);
}

System returns: error: invalid type argument of unary '*' (have 'node {aka struct node}') return (*n);系统返回: error: invalid type argument of unary '*' (have 'node {aka struct node}') return (*n);

Few problems:几个问题:

  1. You have memory leaks.您有 memory 泄漏。
  2. You are referring the variable outside its scope.您指的是 scope 之外的变量。

Right way is:正确的方法是:

    node * node_create( int value, node *left, node *right){
        node *n = malloc(sizeof(node));

        n->leftson = left;
        n->rigthson = right;
        n->val = value;

        return n;
    }

Free the memory once done using and also add memory check after malloc .使用完成后释放 memory 并在 malloc 之后添加malloc检查。

The unary * operator is applied to a pointer to dereference it, ie get the object it points to.一元*运算符应用于指针以取消引用它,即获取它指向的 object。 But n isn't a pointer, it's an instance of a struct node object, so you can't apply * to it.但是n不是指针,它是struct node object 的一个实例,所以不能对它应用*

Also, you don't do anything with the return value of malloc , so all it does is leak memory.此外,您不会对malloc的返回值做任何事情,所以它所做的只是泄漏 memory。

What you want to do is declare n as a pointer to struct node , assign the allocated memory to that pointer, set the values in the pointed-to structure, then return the pointer.您要做的是将n声明为指向struct node的指针,将分配的 memory 分配给该指针,设置指向结构中的值,然后返回指针。

node * node_create( int value, node *left, node *right){
    node *n = malloc(sizeof(node));
    n->value = value;
    n->leftson = left;
    n->rightson = right;
    return n;
}

malloc(sizeof(node));

malloc attempts to allocate memory and return a pointer to that memory. malloc尝试分配 memory 并返回指向该 memory 的指针。 The statement above does not do anything with that return value.上面的语句对该返回值没有任何作用。 You need to assign the result of malloc to a variable, as with node *p = malloc(sizeof *p);您需要将malloc的结果分配给一个变量,如node *p = malloc(sizeof *p); . .

node n = {left, right, value};

This creates a local automatic object named n , which is not what you want.这将创建一个名为n的本地自动 object ,这不是您想要的。 The function node_create is defined to return a pointer to a node , so you need to return a pointer to an object that the caller can use, and an automatic object is not suitable for that (because its memory reservation ends when the function returns). The function node_create is defined to return a pointer to a node , so you need to return a pointer to an object that the caller can use, and an automatic object is not suitable for that (because its memory reservation ends when the function returns).

Instead, after allocating memory and assigning its address to p as shown above, fill in the object at p with the desire values.相反,在分配 memory 并将其地址分配给p之后,如上所示,在p处填写 object 所需的值。 You can use:您可以使用:

p->leftson  = left;
p->rightson = right;
p->value    = value;

return (*n);

*n does not make any sense if n is a node, not a pointer. *n如果n是节点而不是指针,则没有任何意义。 And, if n is a pointer, *n would be the structure it points to.而且,如果n是一个指针, *n将是它指向的结构。 But node_create is defined to return a pointer, not a structure.但是node_create被定义为返回一个指针,而不是一个结构。 So you want to return a pointer.所以你想返回一个指针。

After the code above, you can return the required pointer with return p;在上面的代码之后,您可以使用return p; . .

The function node_create has the return type node * . function node_create具有返回类型node *

node * node_create( int value, node *left, node *right){

This means that the function need to return a pointer.这意味着 function 需要返回一个指针。

The variable n declared like变量n声明为

node n = {left, right, value};

is not a pointer.不是指针。 It has the type struct node .它的类型为struct node So applying the unary indirection operator * for an object that does not have a pointer type因此,对没有指针类型的 object 应用一元间接运算符*

return (*n);

does not make sense.没有意义。

You could use the address operator & like您可以使用地址运算符& like

return (&n);

returning a pointer from the function.从 function 返回一个指针。 But in this case the returned pointer will be invalid because the pointed local variable n will not be alive after exiting the function.但在这种情况下,返回的指针将无效,因为指向的局部变量n在退出 function 后将不再存在。

You need is to allocate an object of the type struct node dynamically.您需要动态分配结构节点类型的 object 。 And you are doing this.而你正在这样做。 However you are not assigning the returned value of call of malloc to any variable and are not returning it from the function.但是,您没有将malloc调用的返回值分配给任何变量,也没有从 function 中返回它。

malloc(sizeof(node));

So there is a memory leak in the function.所以在 function 中有一个 memory 泄漏。 The address of the allocated memory is lost and the memory can not be freed.分配的 memory 的地址丢失,无法释放 memory。

What you need is the following您需要的是以下内容

node * node_create( int value, node *left, node *right)
{
    node *n = malloc(sizeof(node));

    if ( n != NULL )
    {
        n->leftson  = left;
        n->rightson = right;
        n->val      = value;
    }

    return n;
}

Pay attention to that before assigning the arguments to the data members of the allocated object you have to check whether the object was allocated successfully.注意,在将arguments分配给已分配的object的数据成员之前,必须检查object是否分配成功。

    if ( n != NULL )

You can consider also an alternative function definition when the function has one parameter of the type pointer to struct node.当 function 具有指向结构节点的类型指针的一个参数时,您还可以考虑另一种 function 定义。 For example例如

node * node_create( node *init )
{
    node *n = malloc(sizeof(node));

    if ( n != NULL )
    {
        *n = *init;
    }

    return n;
}

To call this function you should "pack" all values in an object of the structure type as for example要调用此 function,您应该“打包”结构类型的 object 中的所有值,例如

node { NULL, NULL, 10 };
node *new_node = node_create( &n );

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

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