我的第一个 C 作业是创建一个队列。我使用的是基于数组的实现而不是链表。
当我尝试编译我的代码时出现以下错误:
Queue.c: In function 'Enqueue':
Queue.c:23: warning: assignment from incompatible pointer type
这是我的代码,如果需要我会提供标题代码:
#include "QueueElement.h"
#include "Queue.h"
#define QUEUE_SIZE 10
struct QueueStruct {
QueueElement *contents[QUEUE_SIZE];
int size;
};
Queue CreateQueue(void) {
Queue q = malloc(sizeof(struct QueueStruct));
q->size = 0;
return q;
}
void DestroyQueue(Queue q) {
free(q);
}
void Enqueue(Queue q, QueueElement *e) {
if (q->size < QUEUE_SIZE) {
q->contents[q->size++] = *e; /* PROBLEM IS HERE */
}
}
非常感谢对这个问题的任何帮助以及任何其他建议。 谢谢大家。
最佳答案
我相信你的意思
q->contents[q->size++] = e;
(没有星号) 因为您要将 QueueElement* 类型的内容分配给 QueueElement*[] 数组。
或者您可以通过更改它来修复它 - 这可能更接近我认为您的意思:-
QueueElement contents[QUEUE_SIZE];
不确定这是否有意义/我完全正确。
关于c - C 的新手 : Storing Structures in an Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824374/