dereferencing pointer to incomplete type - typedef

2019-09-19 12:40发布

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 !

2条回答
我想做一个坏孩纸
2楼-- · 2019-09-19 13:19

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

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

查看更多
三岁会撩人
3楼-- · 2019-09-19 13:43

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!

查看更多
登录 后发表回答