请求成员不是结构或联合

dev*_*nce 0 c struct function qsort

所以我为qsort定义了函数比较,但是以下错误显示:

1.c: In function ‘compare’:
1.c:235:7: error: request for member ‘count’ in something not a structure or union
1.c:235:17: error: request for member ‘count’ in something not a structure or union
1.c:237:12: error: request for member ‘count’ in something not a structure or union
1.c:237:23: error: request for member ‘count’ in something not a structure or union
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?我的意思是,这不像我拼错了名字:<

struct word
{
  char wordy[100];
  int count;
};


int compare(const void* a, const void* b)
{

const struct word *ia = (const struct word *)a;
const struct word *ib = (const struct word *)b;

if(*ia.count>*ib.count)
    return 1;
else if(*ia.count==*ib.count)
    return 0;
else
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

rul*_*lof 5

问题是,ia并且ib是指针const struct word.要访问结构的成员true指向它的指针,我们使用箭头(->)而不是点..

另一件事是确保在声明时拼写结构名称,ia并且ib与上面声明的方法相同.

所以你的代码应该是:

struct word
{
  char wordy[100];
  int count;
};


int compare(const void* a, const void* b)
{

const struct word *ia = (const struct word *)a;
const struct word *ib = (const struct word *)b;

if(ia->count > ib->count)
    return 1;
else if(ia->count == ib->count)
    return 0;
else
    return -1;
}
Run Code Online (Sandbox Code Playgroud)