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:) )
The header
punti.hdeclares the namePuntoas an alias for the pointer typestruct punto *to the incomplete typestruct punto.This header is included in the header
item.hwhere there is declared the typedef nameitemas an alias for the typedef namePuntothat is used in parameter declaration list of the functioneq: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.cin the definition of the functioneqthere are used expressions to access data members of the incomplete structure typestruct puntoAT this point the compiler does not know whether indeed the structure
struct puntocontains the data memberx. 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.