【问题标题】:C incompatible types when assigning to type 'struct Node' from type 'struct Node *'从类型'struct Node *'分配给类型'struct Node'时的C不兼容类型
【发布时间】:2021-02-10 21:23:10
【问题描述】:

我只写了函数,编译的时候总是出现这些错误。 我知道问题出在queue->arr[queue->rear]=item。但是我已经在Queue中定义了我的queue->arrNode*,为什么代码仍然是错误的?

[Error] incompatible types when assigning to type 'struct Node' from type 'struct Node *'
[Error] incompatible type for argument 1 of 'insertValue'
[Note] expected 'struct Node *' but argument is of type 'struct Node'
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Queue{
    int rear;
    int front;
    int size;
    int capacity;
    struct Node* arr;
};

struct Node* newNode(int item){
    struct Node* node=(struct Node*) malloc(sizeof(struct Node));
    node->left= NULL;
    node->right=NULL;
    node->data= item;
    return node;
}

struct Queue* creatQ()
{
    struct Queue* queue= (struct Queue*)malloc(sizeof(struct Queue*));
    queue->rear=0;
    queue->front=0;
    queue->size=0;
    queue->capacity= MAXQUEUE;
    queue->arr=(struct Node*)malloc(queue->capacity*sizeof(struct Node));;
    return queue;
}

void push(struct Queue* queue, struct Node* item)
{
    if(queue->rear==(MAXQUEUE-1)){
        printf("Queue full\n");
    }
    else{
        queue->rear= (queue->rear+1)%queue->capacity;
        queue->arr[queue->rear]=item;
        queue->size++;
    }
}

struct Node pop(struct Queue* queue)
{
    struct Node item;
    if (queue->size==0){
        printf("Empty queue\n");
    }
    else{
        item=queue->arr[queue->front];
        queue->front= (queue->front+1)%queue->capacity;     
        queue->size--;      
        return item;
    }
}

struct Node front(struct Queue* queue)
{
    struct Node item;
    if(queue->size!=0){
        item=queue->arr[queue->front];
        return item;
    }
}

struct Node* insertValue(struct Node* root, int value, struct Queue* queue)
{
    struct Node* node= newNode(value);
    struct Node item= front(queue);
    if (root==NULL){
        root=node;
    }
    else if(item.left==NULL){
        item.left=node;
    }
    else {
        item.right=node;
        pop(queue);
    }
    
    push(queue, node);
    return root;
}

【问题讨论】:

  • 你知道queue->arr[queue->rear]*(queue->arr + queue->rear)是一样的吗?
  • 欢迎使用 stackoverflow。请先拨打tour,以便了解该网站的运作方式。
  • "但是我已经将我的队列->arr 定义为 Node*" 是的,完全正确。这意味着queue->arr[x] 的类型是struct Node,而不是struct Node*
  • 你可能想要queue->arr[queue->rear] = *item;而不是queue->arr[queue->rear] = item;
  • 第二个错误不是你显示的代码引起的。这是由您未显示的对insertValue 的函数调用引起的。

标签: c pointers struct binary-tree binary-search-tree


【解决方案1】:

有很多错误。例如在这个声明中

struct Queue* queue= (struct Queue*)malloc(sizeof(struct Queue*));
                                                  ^^^^^^^^^^^^^

调用 malloc 时使用了不正确的参数。你需要写

struct Queue* queue= (struct Queue*)malloc(sizeof(struct Queue));
                                                  ^^^^^^^^^^^^

或者在函数push里面有一个错误的赋值

queue->arr[queue->rear]=item;

变量item 的类型为struct Node *

void push(struct Queue* queue, struct Node* item)
                               ^^^^^^^^^^^^^^^^^

而指针arr指向的数组元素类型为struct Node

或者如果数据成员大小等于0,函数front什么也不返回。

struct Node front(struct Queue* queue)
{
    struct Node item;
    if(queue->size!=0){
        item=queue->arr[queue->front];
        return item;
    }
}

或者这个错误信息

[Error] incompatible type for argument 1 of 'insertValue'
[Note] expected 'struct Node *' but argument is of type 'struct Node'

表示您在某处调用函数,将struct Node 类型的对象传递给它,而相应参数的类型为struct Node *

struct Node* insertValue(struct Node* root, int value, struct Queue* queue)
                         ^^^^^^^^^^^^^^^^^  

在编写任何新函数之前,请使用先前定义的函数编译您的程序。如果您不确定已经编写的代码是否有效,请不要编写新代码。

请注意,如果您将队列实现为数组,那么将数组元素定义为具有数据成员 leftright 是没有多大意义的。

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

新元素被添加到数组的当前尾部并从其头部弹出。

【讨论】:

  • 我之所以使用struct Node* left struct Node*rightqueue 是因为这个问题的实际问题是:从未知大小的输入按级别顺序构建二叉树。例如 [0,0,0,null,0,0] ,其中第一个 0 是根,第二个 0 是根的左孩子,依此类推。如果我抓住了空值,在这种情况下,它应该是第 3 层中最左边的节点。但是由于它是空值,所以我不会在其中放置任何节点。
  • 由于输入的大小未知,我能想到的唯一方法就是使用队列。或者你有什么更好的想法吗?我是这些数据结构概念的新手......
猜你喜欢
  • 2021-01-17
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多