I want to hide a specified struct in my c module, so struct declaration is in the *.c file and the header contains a typedef. Something like this:
/* member.h */
typedef struct MEMBER_T *member_t;
/* member.c */
#include "member.h"
struct MEMBER_T {
unsigned int member_id;
char name;
};
and later in the c file I want to do a member table:
members = calloc(10, sizeof(member_t));
but it’s wrong, I know.
How can I use thie solution? I mean how can I create an object for member_t?
Thanks in advance!
You are allocating an array. you still need to allocate each of its elements.
If you want to hide the struct in .c file, than it would also be a good idea to encapsulate its creation. So the user of you struct will not ha to bother whether to user
sizeof(member_t)orsizeof(*member_t)orsizeof(struct MEMBER_T).define a function
membet_t* create_member_array(int)in you header in addition totypedef struct MEMBER_T* member_t.implement it in you .c file
the declaration in you struct
char name;combined with the statementmembers[0]->name = "test"is not correct. You declar a single char and try to a ssign a string to it, witch itself is not correct. it should bechar* nameand you shold allocate mememory before trying to copy a string to ist usingstrcpy()alternativ:
This is a lot of memory allocation witch you need to make sure you release / free it. You will have to create another function to free all this memory