Reputation: 2143
I was viewing a header file with the following statement:
typedef struct my_server_t my_server_t;
As I understand about typedef
, they are used to simplify the definitions of data structures and provides an alias for another data type. So in the following statements:
typedef struct {
int port;
} my_server;
my_server my_server_t;
my_server_t
is of type my_server
.
But with no definition of my_server_t
present in the header file, what do I interpret from it?
Upvotes: 3
Views: 174
Reputation: 4954
If you see something like:
typedef struct my_server_t my_server_t;
Then, struct my_server_t
may be defined somewhere, and my_server_t
would be an alias for it.
If you can't find it in the header file you're viewing, then, most likely it will be defined in some other header file that is included in the one you're viewing. Or even, it can be defined in some other header file that is included in one of the header files included in the one you're viewing, etc (consider any level of header files nesting).
Upvotes: -1
Reputation: 222536
If the header (or a translation unit generally) uses struct my_server_t
but does not contain a definition of it, then struct my_server_t
is an incomplete type. The compiler knows it is a structure, but it does not know what is in it or how big it is.
You cannot define an object of this type: struct my_server_t x;
would fail because the compiler does not know how much space to use for x
.
However, you can have a pointer to an incomplete type. The pointers for all structures have the same size and representation, so the compiler has enough information to make a pointer for struct my_server_t
even though it does not know the size of struct my_server_t
. This means you can call a routine defined in another translation unit that does have a complete definition for struct my_server_t
. That routine can create a struct my_server_t
(by using malloc
or by defining a static object or by other means) and return a pointer to it. You can then use the structure by calling other routines that know what to do with it.
Thus, structures without definitions give a library of routines a way to let its users request and use information without revealing to the users the internal details of how the information is maintained.
The definition of my_server_t
as a type name with typedef struct my_server_t my_server_t;
merely provides a convenient name to use for the type. It does not affect the meaning or definition of the structure at all.
Upvotes: 6
Reputation: 18420
You can forward declare a structure with an unknown definition in C and use it, as long as the size or the members are not needed (you can for example use a pointer to it or similar).
The typedef does not change anything, it just says, that my_server_t
is of type struct my_server
. The definition of the struct does not have to be known.
But as soon as you need the definition (i.e. use explicitly or implicitly the size or a member of this struct), you will get an error (incomplete type
) if it is not defined yet.
Upvotes: 6