c - foward typedef structures -
gcc 4.4.4 c89
i have in header file.
port.h
struct struct_tag; int initialize_ports(struct_tag *port);
in implemenation file have this:
port.c
typedef struct struct_tag { int port_id; } port_t;
and in driver.h file, have following:
#include "port.h" int initialize_ports(struct_tag *port) { port = malloc(sizeof *port); /* checking here */ }
i have forward declared structure, want hide internal elements.
however, getting following error on initialize_ports in header file:
expected ‘)’ before ‘*’ token
i wondering how can forward declare , able pass structure parameter?
many advice,
as other answers have noted, could change struct_tag
struct struct_tag
in prototype. way of getting code compile write
typedef struct struct_tag struct_tag;
in place of existing struct struct_tag;
(i.e. combine typedef forward definition). allow write
int initialize_ports(struct_tag *port)
without compile failures. however, still not quite want, because caller can neither allocate local variable of type, nor malloc() 1 - because don't know size.
other answers have suggested should open definition of structure. that's not right answer - because removes abstraction layer you're trying create. better have functions (in port.c
, i.e. library does know internals) such as:
struct_tag *create_port(...); void free_port(struct_tag *port)
i.e. create , free structures - , indeed other operations (such reading / writing structure) too.
Comments
Post a Comment