不能将“void”类型的值分配给“void(*)类型的实体(struct*Queue,int)

不能将“void”类型的值分配给“void(*)类型的实体(struct*Queue,int),c,struct,function-pointers,C,Struct,Function Pointers,我有以下结构: typedef struct{ int *arr; int maxSize, curSize; int first, last; int(*isEmptyFunc)(Queue); int(*isFullFunc)(Queue); void(*EnqueueFunc)(struct Queue*, int); void(*DequeueFunc)(struct Queue*); int(*TopFunc)(Queue

我有以下结构:

typedef struct{
    int *arr;
    int maxSize, curSize;
    int first, last;
    int(*isEmptyFunc)(Queue);
    int(*isFullFunc)(Queue);
    void(*EnqueueFunc)(struct Queue*, int);
    void(*DequeueFunc)(struct Queue*);
    int(*TopFunc)(Queue);
} Queue;
以及一个create queue函数,该函数返回指向新队列的指针:

    int *arr = malloc(sizeof(int) * size);
    isNull(arr);
    Queue *q = malloc(sizeof(Queue));
    isNull(q);
当我尝试为函数指针赋值时,我会: 这一切都发生在CreateQueue函数中

q->isEmptyFunc = isEmpty(*q);
q->isFullFunc = isFull(*q);
q->TopFunc = Top(*q);
q->DequeueFunc = Dequeue(q);
实际函数在.c文件顶部包含的头文件I中声明,并在CreateQueue func下面实现。 前三个赋值看起来不错,但对于第四个赋值,编译器会尖叫:

IntelliSense: a value of type "void" cannot be assigned to an entity of type "void (*)(struct Queue *)" 
出列功能的实现是:

void Dequeue(Queue *q) {
    if (q->isEmptyFunc()) return;
    q->first = (q->first + 1) % (q->maxSize);
    q->curSize--;
}

这里发生了什么?

这里的主要问题是,isEmptyFunc、isFullFunc、EnqueueFunc和DequeueFunc都是函数指针。您试图在这里放置函数调用的返回值,我们可以假设它不是函数指针。这是完全错误的。这不好,您不应该这样做

现在,如果我们看到,在你的情况下

前三个作业看起来不错

编译器在这里没有抱怨,因为所有三个函数调用都返回一些值,可能是int?,并且该值被隐式转换为函数指针类型,但是,行为没有定义。您不能这样做

但对于第四个问题,编译器尖叫道:

IntelliSense: a value of type "void" cannot be assigned to an entity of type "void (*)(struct Queue *)" 
在这种情况下,出列函数返回类型为void,不能用作值。因此,编译器很感激地抱怨

您需要更改上述所有语句。

Dequeue是一个函数指针,Dequeueq是函数调用的结果。这完全不是一回事。