gcc 4.4.4 c89
I have this in my header file.
port.h
struct struct_tag;
int initialize_ports(struct_tag *port);
In my implemenation file I have this:
port.c
typedef struct struct_tag {
int port_id;
} Port_t;
And in my driver.h file, I have the following:
#include "port.h"
int initialize_ports(struct_tag *port)
{
port = malloc(sizeof *port);
/* do checking here */
}
I have forward declared the structure, as I want to hide the internal elements.
However, I am getting the following error on my initialize_ports in the header file:
expected ‘)’ before ‘*’ token
I am just wondering how can I forward declare and be able to pass the structure as a parameter?
Many thanks for any advice,
As other answers have noted, you could change
struct_tagtostruct struct_tagin the prototype. Another way of getting your code to compile is to writein place of your existing
struct struct_tag;(i.e. combine the typedef with the forward definition). That then does allow you to writewithout compile failures. However, this is still not quite what you want, because the caller can neither allocate a local variable of this type, nor malloc() one – because they don’t know the size.
Other answers have suggested that you should open up the definition of the structure. That’s generally not the right answer – because it removes the abstraction layer you’re trying to create. Much better to have functions (in the
port.c, i.e. the library that does know about the internals) such as:i.e. to create and free the structures – and indeed for other operations (such as reading from / writing to the structure) too.