【问题标题】:Unable to dequeue elements from my queue "Invalid use of void Expression"无法从我的队列“无效使用无效表达式”中取出元素
【发布时间】:2021-05-25 12:55:21
【问题描述】:

我从一些旧的 repo 获得了一个队列文件,没有任何文档,我试图从中排队和取消队列数据

.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 数组加入队列

我正在使用所需的信息(例如大小、命令和实际数据)更新我的结构数据

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

然后我调用入队函数

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

我正在尝试出列数据

在这里,我将数据转换为发送方数据的结构

但此时我收到错误,因为无效使用无效表达式

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

【问题讨论】:

    标签: c struct void


    【解决方案1】:

    在这一行:

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

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

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

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

    或者使用解引用操作符代替数组索引:

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

    【讨论】:

    • @RHKP 您已将data_to_send 定义为无效的零长度数组。将大小更改为 1 或使其不是数组并在需要时传递其地址。
    • 我把数组部分改成了data_pack *data_to_send;并将更新部分更改为 - data_to_send->message=20; data_to_send->in_array=&data;试图在这个时候运行我得到-- 0 [main] nwudp 908 cygwin_exception::open_stackdumpfile: Dumping stack trace to nwudp.exe.stackdump
    • @RHKP 现在你正在取消引用一个空指针,因为data_to_send 没有指向任何地方。使其不是指针。
    • 对不起,我是 C 新手,您的意思是 data_pack data_to_send;
    • @RHKP 是的,它目前是一个未初始化的指针。让它不是指针。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多