Reputation: 1159
I have a header file:
#ifndef LOCATION_H
#define LOCATION_H
#define MAX_X_GRID (3)
#define MAX_Y_GRID (3)
typedef struct _location Location;
extern Location *Location_create();
extern void Location_destroy(Location *);
#endif /* LOCATION_H */
.. a soure file:
#include <stdlib.h>
#include "Location.h"
#define VALID_LOCATION_CODE (245)
#define _location_check(l) \
if ( l == NULL || l->_Valid != VALID_LOCATION_CODE ) \
abort();
struct _location
{
int x;
int y;
int _Valid;
};
struct _location *Location_create(const int x, const int y)
{
if (x > MAX_X_GRID || y > MAX_Y_GRID)
return NULL;
struct _location *l = malloc(sizeof(struct _location));
if (l == NULL)
return NULL;
l->x = x;
l->y = y;
l->_Valid = VALID_LOCATION_CODE;
return l;
}
void Location_destroy(struct _location *l)
{
_location_check(l);
free(l);
}
.. and I am testing the code like that:
#include <stdio.h>
#include "Location.h"
int main(int argc, char const *argv[])
{
Location *l = Location_create(1, 2);
printf("x: %d, y: %d\n", l->x, l->y); /* line 6 */
Location_destroy(l);
return 0;
}
When I compile the program with this command:
gcc test.c -o test -Wall
I get these errors:
test.c:6:27: error: trying to dereference a pointer of incomplete type
// line 6
test.c:6:33: error: trying to dereference a pointer of incomplete type
// line 6
From the errors, it seems that gcc is not aware of the header file I included.
What can I do to fix that ?
Upvotes: 0
Views: 901
Reputation: 7923
Location.h
doesn't disclose the contents of the struct _location
(type is incomplete), so while compiling test.c
gcc has no clue what l->x
and l->y
are.
Upvotes: 2
Reputation: 84642
You need to move the struct
and function declarations
(not definitions) into the header. E.g. move the following into the header file:
#ifndef LOCATION_H
#define LOCATION_H
#define MAX_X_GRID (3)
#define MAX_Y_GRID (3)
typedef struct _location Location;
struct _location
{
int x;
int y;
int _Valid;
};
struct _location *Location_create(const int x, const int y);
void Location_destroy(struct _location *l);
#endif /* LOCATION_H */
Upvotes: 0