错误:表达式不能用作函数.如何删除它

Error: expression cannot be used as a function. How to remove this?

本文关键字:何删除 删除 函数 表达式 不能 错误      更新时间:2023-10-16

我正在使用这个结构

    struct box
    {
        int h,w,d;
    };
    int compare (const void *a, const void * b)
    {
        return  ((*(box *)b).d * (*(box *)b).w) – ((*(box *)a).d * (*(box *)a).w); // error is showing in this line
    }
    box rot[3*n];
    qsort (rot, n, sizeof(rot[0]), compare);

我正在尝试 qsort但显示错误表达式不能用作函数

返回行中的减号运算符有问题,应该-而不是

定义数组时的另一个问题rot数组应包含类型struct box而不是box的元素,因为结构块中没有typedef

因此,您面临两种使其工作的可能性,要么为结构box添加一个标签box

typedef struct box
{
    int h,w,d;
} box;

或者简单地在数组的定义中添加单词 struct rot 并在比较函数中像这样(在每个box单词之前):

int compare (const void *a, const void * b)
{
    return  ((*(struct box *)b).d * (*(struct box *)b).w) - ((*(struct box *)a).d * (*(struct box *)a).w);
}

struct box rot[3*n];