Вопрос

so I have function compare defined for qsort, but the following error shows:

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

Does anyone know why? I mean it's not like I misspelled the name :<

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;
}
Это было полезно?

Решение

The problem is that ia and ib are pointers to const struct word. To access a member of a structure true a pointer to it we use arrow (->)and not a dot ..

Another thing make sure to spell the structre name when you declare ia and ib the same way as you declared it in the above.

So you code whould be:

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;
}

Другие советы

And yet, you did misspell the name :(

You're referring to words in the compare function and defining word outside of it.

[edit]
You say it's defined as a global struct. Where? The source you copied in here has no discoverable definition of words
[/edit]

Since you've edited the post from it's original form, your problem is now as rullof has posted - you're accessing -> items using .

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top