I am allocating pointer to head structure using malloc, but I would like to have 'title' field have size of 100 char elements. How to properly do it?
struct tail{
int x;
};
struct head{
char *title;
struct tail *next;
};
void function(){
struct head *h;
//this (program won't know how much to allocate for 'title':
h = (struct head*)malloc(sizeof(struct head));
//VS this (Segmentation fault (core dumped)):
h->title = (char*)malloc(sizeof(char)*255);
h->next = (struct tail*)malloc(sizeof(struct tail*));
/*
or maybe I should alloc h, then realloc title?
If that's the answer, how will I free whole structure later?
*/
}
ps. I am not using char title[100]
on purpose.
edit note. (char) was a typo, I have (char*) in my code