I have two header files, namely point.h and polygon.h
//point.h
#ifndef POINT_H
#define POINT_H
typedef struct Point point;
point *alloc_point(void);
int free_point(point *);
void init_point(point *, float, float);
void print_point(point *);
#endif
and
//polygon.h
#ifndef POLYGON_H
#define POLYGON_H
typedef struct Polygon polygon;
polygon *alloc_polygon(void);
int free_polygon(polygon *);
void init_polygon(polygon *, unsigned, point *);
void print_polygon(polygon *);
#endif
with the corresponding point.c and polygon.c files
//point.c
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
struct Point
{
float x;
float y;
};
point *alloc_point(void)
{
point *pt = (point *)malloc(sizeof(point));
if (pt)
{
return pt;
}
else
{
fprintf(stderr, "Could not allocate point. Aborting\n");
exit(EXIT_FAILURE);
}
}
int free_point(point *pt)
{
if (pt)
{
free(pt);
pt = NULL;
return 1;
}
else
{
fprintf(stderr, "Could not free point. Aborting\n");
return 0;
}
}
void init_point(point *pt, float x, float y)
{
pt->x = x;
pt->y = y;
}
void print_point(point *pt)
{
printf("Point at (%f, %f)\n", pt->x, pt->y);
}
and
//polygon.c
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "polygon.h"
struct Polygon
{
unsigned nside;
point *centre;
};
polygon *alloc_polygon(void)
{
polygon *poly = (polygon *)malloc(sizeof(polygon));
if (poly)
{
return poly;
}
else
{
fprintf(stderr, "Cannot allocate polygon. Aborting\n");
exit(EXIT_FAILURE);
}
}
int free_polygon(polygon *poly)
{
if (poly)
{
free(poly);
poly = NULL;
return 1;
}
else
{
fprintf(stderr, "Cannot free polygon. Aborting\n");
exit(EXIT_FAILURE);
}
}
void init_polygon(polygon *poly, unsigned nside, point *centre)
{
poly->nside = nside;
poly->centre = centre;
}
void print_polygon(polygon *poly)
{
printf("%u-sided polygon with centre at (%f, %f)",
poly->nside, poly->centre->x, poly->centre->y);
}
When I try to run main.c, which contains
#include <stdio.h>
#include "point.h"
#include "polygon.h"
int main() {
point *centre = alloc_point();
init_point(centre, 10.0, 10.0);
print_point(centre);
unsigned nside = 4;
polygon *poly = alloc_polygon();
init_polygon(poly, nside, centre);
print_polygon(poly);
free_point(centre);
free_polygon(poly);
return 0;
}
I get the error message (coming from the print_polygon method inside polygon.c)
error: dereferencing pointer to incomplete type 'point' {aka 'struct Point'}
I do not get that error as the definition of the Polygon structure has a pointer to point. Why can I not get the code running?
P.S.: I use gcc 8.1.0 and compile using
gcc -Os -Wall -Wextra -Wpedantic -Werror -std=c99 .\main.c .\point.c .\polygon.c -o .\main.exe
In
poly -> centre -> x,poly -> centreis a pointer to apoint, and-> xdereferences thatpoint, butpolygon.cdoes not have a complete definition ofpoint. It only knowspointisstruct Pointbut not what the contents ofstruct Pointare.Some options to fix this are:
point.cthat will print the point’s coordinates, export that routine frompoint.c(viapoint.h), and call it fromprint_polygon.point.cthat will provide the coordinates, export that routine or routines, and call it or them fromprint_polygonto get the values to print.struct Pointvisible inpolygon.The first is best for preserving modularity.
(In 2, the coordinates could be provided by returning them from one routine in a structure whose definition is shared between
point.candpolygon.c, by returning the twofloatcoordinates in separate routines, one for each, or by returning the twofloatcoordinates infloatobjects passed by reference.)