我是 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/