cpointersdeclarationtypedefincomplete-type

Pointer to incomplete class type "struct punto" is not allowed


I'm trying to create a code that creates a list of points. I have a file named "punti.c" with its header and a file named "item.c" with its header. The problem is that I can't access the variables of the struct point(x,y) from the item.

//item.c
#include <stdio.h>
#include "item.h"
#include "punti.h"

int eq(item it1, item it2)
{
    return it1->x == it2->x; //the problem is here
}
//item.h
#include "punti.h"
typedef Punto item;

int eq(item x, item y);
//punti.c
#include "utilities.h"
#include "punti.h"
#include <math.h>

struct punto
{
    double x;
    double y;
};
//punti.h
typedef struct punto *Punto;

I tested the type Punto in many ways, so I'm sure it works. I tried to change the typedef of item, made it a pointer to Punto, but it didn't work. (Part of the code is in Italian, sorry:) )


Solution

  • The header punti.h declares the name Punto as an alias for the pointer type struct punto * to the incomplete type struct punto.

    //punti.h
    typedef struct punto *Punto;
    

    This header is included in the header item.h where there is declared the typedef name item as an alias for the typedef name Punto that is used in parameter declaration list of the function eq:

    //item.h
    #include "punti.h"
    typedef Punto item;
    
    int eq(item x, item y);
    

    The both typedef names Punto and item still denote pointer types to an incomplete structure type. They may be used in the function declaration because pointers themselves are always complete types.

    But then in the module item.c in the definition of the function eq there are used expressions to access data members of the incomplete structure type struct punto

    //item.c
    #include <stdio.h>
    #include "item.h"
    #include "punti.h"
    
    int eq(item it1, item it2)
    {
        return it1->x == it2->x; //the problem is here
    }
    

    AT this point the compiler does not know whether indeed the structure struct punto contains the data member x. SO it issues an error. You need to include the complete structure declaration in modules where there are attempt to access data members of the structure. Otherwise the compiler will report errors.