I have this struct in a file “vector.c”:
struct Vector
{
int * m_items;
int m_size;
int m_extSize;
int m_numItems;
};
On “main.c” I’m trying to check if the value m_items of a certain vector is NULL :
if (! vec->m_items)
printf("not fail\n");
I do it after initializing “vec” with values – the vector has 1 value (I checked it).
However, gcc is writing an error for the above line :
Error: dereferencing pointer to incomplete type
Why is it?
You have to move the entire definition of
Vectorto a header file and include it in both vector.c and main.c.Adding a
typedef struct Vector Vector;is not enough. That merely tells the compiler that there is a typeVectorand is defined elsewhere, so, it is incomplete. It lets you declare pointers to it because it doesn’t need to know which members it has in order to allocate a pointer. All pointers are the same size.