tags:

views:

71

answers:

5

Hello,

gcc 4.4.4 c89

Not sure why I am getting this error.

In my header file I 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,

+2  A: 

Your struct's name needs to match the typedef:

struct Handle_t {
    /* ... */
};
John Millikin
+1  A: 
typedef struct Handle_t Handle

The struct you defined is called Handle, not Handle_t, which is what you're typedeffing.

Artefacto
+1  A: 

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.

Anil Vishnoi
+3  A: 

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 * */
pmg
+1 for mnemonic
teukkam
Just one confusing thing about typedefing. typedef struct Handle Handle_t. The 'struct Handle' part is the type. And the Handle_t is the definition. So the struct is actually called Handle_t. Would that be correct? Thanks.
robUK
Not quite robUK. The `struct MYSTRUCT` does not create an identifier `MYSTRUCT`: it creates an (pseudo-)identifier `struct MYSTRUCT`. *I believe the struct works as you describe in `C++`.*
pmg
+1  A: 

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.

pmg