I was struggling to fix a code today, then I come across something similar to:
typedef struct {
int a;
int b;
int c;
int d;
char* word;
} mystruct;
int main(i
malloc the outer struct will only allocate 1 byte memory pointed by *word
since it is a 'char *' type. If you want to allocate more than 1 byte of memory pointed by word
, there are 2 options:
char word[50]
instead of `char *'word
as well. Remember to call free
twice as well in this case.