无法从我的队列 "Invalid use of void Expression" 中取出元素

Unable to dequeue elements from my queue "Invalid use of void Expression"

我从一些没有任何文档的旧回购中得到了一个队列文件,我试图从中入队和出队数据

.h文件如下

#include"limits.h"
#include"stdio.h"
#include"pthread.h"
#include"unistd.h"
#include"stdlib.h"
#include"string.h"
#include"time.h"
pthread_mutex_t lock;


typedef struct{
    int* message;
    void*in_array;
}data_pack;

 data_pack* createpack()
{

     data_pack* pack = malloc(sizeof( data_pack));
     return pack;
}

// Queue Structure
struct Queue {
    int front, rear, size; // front, rare and the size of the Queue
    unsigned long long capacity; // stores the capacity of the Queue
    data_pack** array; //2D array to store the array of arrays
    int* array_sizes; //1D array to store the number of elements of each array
    data_pack** remArray; //2D array to store the remaining array of arrays(used for flush)
};


// Creates a Queue
struct Queue* createQueue()
{
    struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
    queue->front = queue->size = 0;
    queue->capacity = queue->size + 1;
    queue->rear = queue->capacity - 1;
    queue->array = (data_pack **)malloc(queue->capacity * sizeof(data_pack));// changed to my struct
    queue->array_sizes = (int *)malloc(queue->capacity * sizeof(int));



    if (pthread_mutex_init(&lock, NULL) != 0) {
            printf("\n mutex init has failed\n");
            return 0;
        }
    return queue;
}

// Queue is empty when size is 0
int isEmpty(struct Queue* queue)
{

    return (queue->size == 0);

}

// Function to add an array to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, data_pack arr[], int n)
{
    int i=0;
    pthread_mutex_lock(&lock);// lock variables
    queue->rear = (queue->rear + 1) % queue->capacity;
    queue->array[queue->rear] = (data_pack *)malloc(n*sizeof(data_pack));
    if(queue->array[queue->rear]==NULL) printf("Error in memory\n");
    for(i=0;i<n;i++){
        queue->array[queue->rear][i] = arr[i];// u need to input struct here

    }
    queue->array_sizes[queue->rear] = n;
    queue->size = queue->size + 1;
    queue->capacity = queue->capacity + 1;
    queue->array = (data_pack**)realloc(queue->array,queue->capacity*sizeof(data_pack));
    if(queue->array == NULL) printf("Not 2d array");
    queue->array_sizes = (int*)realloc(queue->array_sizes,queue->capacity*sizeof(int));
    if(queue->array_sizes == NULL) printf("not 1dn array sizes");
    pthread_mutex_unlock(&lock);// unlock variables

}

// Function to remove an array from queue.
// It changes front and size
data_pack* dequeue(struct Queue* queue)
{
    int i=0;
    pthread_mutex_lock(&lock);// lock variables
    int len = queue->array_sizes[queue->front];
    data_pack* deqArray = (data_pack*)malloc(len*sizeof(data_pack));
    if (isEmpty(queue))
        return 0;

    for( i=0;i<queue->array_sizes[queue->front];i++){

        deqArray[i] = queue->array[queue->front][i];
    }


    queue->front = (queue->front + 1) % queue->capacity;
    queue->size = queue->size - 1;
    pthread_mutex_unlock(&lock);// unlock variables
    free(queue->array[queue->front-1]);

    return deqArray;
}

// Function to flush all the arrays and its elements from the queue.
data_pack** flush(struct Queue* queue)
{
    int i=0;
    int j=0;
    queue->remArray = (data_pack **)malloc(queue->size * sizeof(data_pack));
    int k=0;
    for( i=queue->front;i<=queue->rear;i++){
        int len = queue->array_sizes[i];
        queue->remArray[k] = (data_pack *)malloc(len*sizeof(data_pack));
        for( j=0;j<len;j++){

            //queue->remArray[k][j] = queue->array[i][j];
        }
        free(queue->array[i]);

        k++;
    }
    free(queue->array);
    free(queue->array_sizes);
    queue->front = queue->size = 0;
    queue->capacity = queue->size + 1;
    queue->rear = queue->capacity - 1;
    queue->array_sizes = (int *)malloc(1 * sizeof(int));
    queue->array = (data_pack **)malloc(1 * sizeof(data_pack));
    return queue->remArray;
}

// Function to know number of arrays in queue
int no_elements(struct Queue* queue)
{

    return queue->size;


}

在我的代码中,我尝试对元素进行入队、获取队列大小和出队元素

struct Queue * send_data_queue;
data_pack data_to_send[0];
data_pack * rec_data;
typedef struct SenderData {
  unsigned short CommandCode;
  int DataSize;
  void * Data;
}
SenderData;

int cont[200];

SenderData data, dat;

int main(int argc, char * argv[]) {

  send_data_queue = createQueue();

  while (1) {
    data.DataSize = sizeof(cont);
    data.Data = & cont;
    data.CommandCode = 20;

    data_to_send[0].message = 20;
    data_to_send[0].in_array = & data;

    enqueue(send_data_queue, data_to_send, 1);

    int nos = no_elements(send_data_queue);
    printf("No of elements in q = %d\n", nos); // this is printing correctly

    rec_data = dequeue(send_data_queue);

    dat = ((SenderData * )(rec_data -> in_array)[0]); // i get error here Invalid use of void Expression

  }

  return 0;
}

解释我的代码

-我正在尝试将一个数组加入队列,该数组是 200 个元素的 int 数组

我正在用大小、命令和实际数据等所需信息更新我的结构数据

这个队列使用一个结构来传递数据,所以我将我的结构数据更新为这个结构

然后调用入队函数

然后我检查队列大小,它是否正确更新

我正在尝试使数据出队

这里我将数据转换为发件人数据结构

但在这一点上,我收到错误消息,因为 Void 表达式的使用无效

为什么会出现这样的错误?

这一行:

dat = ((SenderData * )(rec_data -> in_array)[0]);

数组下标运算符的优先级高于类型转换运算符。所以首先要评估的是rec_data -> in_array[0]。这取消引用了不允许的 void *

您要么需要先使用括号来应用强制转换:

dat = ((SenderData *)(rec_data->in_array))[0];

或者使用取消引用运算符代替数组索引:

dat = *(SenderData *)rec_data->in_array;