c - 请求成员 .. 不是结构

标签 c arrays function pointers compare

我是 c 编程的新手,我不知道为什么我会收到标题中提到的错误。我正在尝试编写一个函数来比较结构中的年龄数组。这是结构:

typedef struct
{
    int index;
    float age;

} index;

这是函数,我收到错误的代码部分:

int comp ( const void *a, const void *b)
{
    if ((((index*) a)->age)>(((index*) b->age)))
        return 1;
    else if ((((index*)a)->age)<(((index*)b)->age))
        return -1;
    return 0;

}

我打算将 comp 函数与 qsort 一起使用。请问我为什么会收到此错误的任何想法?

最佳答案

在你的代码中

 (((index*) b->age)))

应该是

(((index*) b)->age))
           ^^^------------------added parenthesis here

否则,(您可能已经知道)由于 operator precedence , Actor 将在这里无效。

关于c - 请求成员 .. 不是结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53960754/

相关文章:

c++ - 我是否需要断开从 XGetXCBConnection 获得的 xcb_connection_t?

c# - 为什么数组需要在定义时设置维度?

python - numpy 数组操作的奇怪行为

ios - AnchroPoint x :0, y:0 不适用于方程式

arrays - Powershell 2 : Array size differs between return value and caller

php - jQuery/Ajax - 关注/取消关注按钮,重复功能

javascript - 从一组文件名中预加载一堆图像,完成 50% 后执行功能

c++ - Arduino 中不需要的符号扩展

c - 为什么这个C程序在Windows中给出o/p而不是在Linux中给出o/p?

通过引用 voodoo 的 C 字符串