dereferencing pointer to incomplete type
gcc 4.4.4 c89
Not sure why I am getting this error.
In my header file 开发者_StackOverflowI have the following
handle.h
typedef struct Handle_t Handle
In my implementation file
handle.c
struct Handle {
size_t id;
char *name;
};
Handle* create_handle(size_t id)
{
Handle *hdev = NULL;
hdev = malloc(sizeof(*hdev)); /* Error */
.
.
}
Many thanks for any suggestions,
I used to get the typedef
wrong often until I started to think of it like two parts: type and def. The type first, then the definition of a new name.
typedef <type> <name>;
typedef struct Handle Handle_t; /* defines Handle_t as struct Handle */
typedef char *c_string; /* defines c_string as char * */
Your struct's name needs to match the typedef:
struct Handle_t {
/* ... */
};
typedef struct Handle_t Handle
The struct you defined is called Handle
, not Handle_t
, which is what you're typedeffing.
typedef struct Handle_t Handle
you type defined struct Handle_t to Handle, but the structure is Struct Handle,so either change struct Hadle to Struct handle_t or type def.
struct Handle_t
is not defined by the time the compiler reaches the line with the error.
you have defined a struct Handle
and aliased the type Handle
to the inexistent type struct Handle_t
. It's ok to have inexistent types for lots of things in C
, but not for a sizeof
argument.
精彩评论