dereferencing pointer to incomplete type - typedef

2019-09-19 12:58发布

问题:

I am looking for an error in my code (in C) and I am not finding anything. I've looked on many blogs and tried many things that were advised but nothing helped.
I've coded that :

typedef struct Account_t *Account;
struct Account_t {
     Customer customer;
     Realtor realtor;
     Offer offer;
};

while Realtor, Customer and Offer are well defined and included with a .h file. I am getting an error that says "dereferencing pointer to incomplete type 'struct Account_t' " when I write:

 Account account = malloc(sizeof(*account));

Please help me find the problem !

回答1:

I'd say, you have to do something like this:

Account account = (Account)malloc(sizeof(*account));



回答2:

I tried your code out. And I had similar issue as you.

The problem is with your code, when you allocate the structure in the heap, the returned value is wrong assigned, because the malloc returns a pointer.

my code looks like this:

Account* account = (account*)malloc(sizeof(account));

And second thing: As type definition I do not define the structure as a pointer.

typedef struct account_t Account;

Try this, I hope this information useful!