dereferencing pointer to incomplete type

S

Steven

Hello,

I get the following message during compilation:
`dereferencing pointer to incomplete type'

It stems from the compare function I use with qsort() and I am not
quite sure how to fix it.

I have a struct like:
struct node {
time_t utc;
};

And the code that generates the error during compilation:
int cmputc(const void *x, const void *y) {
struct tnode * const *a = x;
struct tnode * const *b = y;
int retv = 0;

if((*a)->utc < (*b)->utc)
retv = -1;
else if((*a)->utc == (*b)->utc)
retv = 0;
else
retv = 1;

return retv;
}

Does anyone know what I am doing wrong ?

Thank you for your explanation.

Steven.
 
S

Steven

Ooopss..
Sorry. already solved..... :) It's in the name using node in the
structure declaration and tnode in the compare function .. aiiii..

Thankx anyway.. [blush]
 
E

Emmanuel Delahaye

Steven a écrit :
Be sure that this structure definition ...
struct node {
time_t utc;
};

.... is visible from ...
int cmputc(const void *x, const void *y) {
struct tnode * const *a = x;
struct tnode * const *b = y;

.... this function. Typically, it sould be placed in a header and
included where you need it.
 
C

Christopher Benson-Manica

Steven said:
I have a struct like:
struct node {
time_t utc;
};
Right...

int cmputc(const void *x, const void *y) {
struct tnode * const *a = x;
struct tnode * const *b = y;

So what's a struct tnode? Perhaps this is related to your problem?
 
D

dwks

int cmputc(const void *x, const void *y) {
struct tnode * const *a = x;
struct tnode * const *b = y;

You can't dereference a void pointer; you need to cast it.

int cmputc(const void *x, const void *y) {
struct tnode * const *a = (struct tnode * const *)x;
struct tnode * const *b = (struct tnode * const *)y;
 
F

Flash Gordon

dwks wrote:

Please leave in attributions so we can see who you are replying to.
You can't dereference a void pointer; you need to cast it.

The above is not dereferencing anything, it is assigning and is
perfectly valid in C. It is C++ which does not allow it, but that is a
different language.
int cmputc(const void *x, const void *y) {
struct tnode * const *a = (struct tnode * const *)x;
struct tnode * const *b = (struct tnode * const *)y;

Your suggestion above is far harder to read, so I would recommend
sticking with the perfectly legal option of not casting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top